Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@
},
"homepage": "https://github.com/expo/expo-server-sdk-node#readme",
"dependencies": {
"node-fetch": "^2.6.0",
"promise-limit": "^2.7.0",
"promise-retry": "^2.0.1"
"promise-retry": "^2.0.1",
"undici": "^7.2.0"
},
"devDependencies": {
"@tsconfig/node20": "20.1.6",
"@tsconfig/strictest": "2.0.5",
"@types/node": "22.17.2",
"@types/node-fetch": "2.6.12",
"@types/promise-retry": "1.1.6",
"eslint": "9.33.0",
"eslint-config-universe": "15.0.3",
Expand Down
37 changes: 27 additions & 10 deletions src/ExpoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
* Application Services
* https://expo.dev
*/
import fetch, { Headers, Response as FetchResponse } from 'node-fetch';
import assert from 'node:assert';
import { Agent } from 'node:http';
import { gzipSync } from 'node:zlib';
import promiseLimit from 'promise-limit';
import promiseRetry from 'promise-retry';
import { fetch, Agent, Response } from 'undici';

import {
defaultConcurrentRequestLimit,
Expand Down Expand Up @@ -227,13 +226,31 @@ export class Expo {
requestHeaders.set('Content-Type', 'application/json');
}

const response = await fetch(url, {
method: options.httpMethod,
body: requestBody,
headers: requestHeaders,
agent: this.httpAgent,
const headersRecord: Record<string, string> = {};
requestHeaders.forEach((value, key) => {
headersRecord[key] = value;
});

const fetchOptions: {
method: 'get' | 'post';
headers: Record<string, string>;
body?: string | Buffer;
dispatcher?: Agent;
} = {
method: options.httpMethod,
headers: headersRecord,
};

if (requestBody) {
fetchOptions.body = requestBody;
}

if (this.httpAgent) {
fetchOptions.dispatcher = this.httpAgent;
}

const response = await fetch(url, fetchOptions);

if (response.status !== 200) {
const apiError = await this.parseErrorResponseAsync(response);
throw apiError;
Expand All @@ -257,7 +274,7 @@ export class Expo {
return result.data;
}

private async parseErrorResponseAsync(response: FetchResponse): Promise<Error> {
private async parseErrorResponseAsync(response: Response): Promise<Error> {
const textBody = await response.text();
let result: ApiResult;
try {
Expand All @@ -275,7 +292,7 @@ export class Expo {
return this.getErrorFromResult(response, result);
}

private async getTextResponseErrorAsync(response: FetchResponse, text: string): Promise<Error> {
private async getTextResponseErrorAsync(response: Response, text: string): Promise<Error> {
const apiError: ExtensibleError = new Error(
`Expo responded with an error with status code ${response.status}: ` + text,
);
Expand All @@ -288,7 +305,7 @@ export class Expo {
* Returns an error for the first API error in the result, with an optional `others` field that
* contains any other errors.
*/
private getErrorFromResult(response: FetchResponse, result: ApiResult): Error {
private getErrorFromResult(response: Response, result: ApiResult): Error {
const noErrorsMessage = `Expected at least one error from Expo`;
assert(result.errors, noErrorsMessage);
const [errorData, ...otherErrorData] = result.errors;
Expand Down
Loading