Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-4485] Use native fetch, drop Node 16 support #906

Merged
merged 7 commits into from
Aug 16, 2023
Merged
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
173 changes: 68 additions & 105 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"homepage": "https://github.com/auth0/node-auth0",
"dependencies": {
"jose": "^4.13.2",
"node-fetch": "^3.3.1",
"uuid": "^9.0.0"
},
"devDependencies": {
Expand All @@ -72,6 +71,7 @@
"husky": "^3.0.1",
"jest": "^29.5.0",
"nock": "^13.2.7",
"node-fetch": "^3.3.1",
"prettier": "^2.8.7",
"pretty-quick": "^1.11.1",
"ts-jest": "^29.1.0",
Expand Down
2 changes: 1 addition & 1 deletion playground/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ export async function jobs() {
const ids = [];

const { data: createImportJob } = await mgmntClient.jobs.importUsers({
users: fs.createReadStream(usersFilePath),
users: new Blob([fs.readFileSync(usersFilePath)], { type: 'application/json' }),
connection_id: connection.id as string,
});

Expand Down
32 changes: 3 additions & 29 deletions src/lib/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { ReadStream } from 'fs';
import { retry } from './retry.js';
import { FetchError, RequiredError, TimeoutError } from './errors.js';
import {
Expand All @@ -10,17 +9,6 @@ import {
FetchAPI,
} from './models.js';

/**
* @private
*/
const nodeFetch = (...args: Parameters<FetchAPI>) =>
import('node-fetch').then(({ default: fetch }) => (fetch as FetchAPI)(...args));

/**
* @private
*/
export const getFormDataCls = async () => import('node-fetch').then(({ FormData }) => FormData);

export * from './models.js';

/**
Expand All @@ -43,7 +31,7 @@ export class BaseAPI {
}

this.middleware = configuration.middleware || [];
this.fetchApi = configuration.fetchApi || nodeFetch;
this.fetchApi = configuration.fetchApi || fetch;
this.parseError = configuration.parseError;
this.timeoutDuration =
typeof configuration.timeoutDuration === 'number' ? configuration.timeoutDuration : 10000;
Expand Down Expand Up @@ -96,11 +84,10 @@ export class BaseAPI {
})),
};

const { Blob } = await import('node-fetch');
const init: RequestInit = {
...overriddenInit,
body:
(await isFormData(overriddenInit.body)) ||
overriddenInit.body instanceof FormData ||
overriddenInit.body instanceof URLSearchParams ||
overriddenInit.body instanceof Blob
? overriddenInit.body
Expand Down Expand Up @@ -184,11 +171,6 @@ export class BaseAPI {
};
}

async function isFormData(value: unknown): Promise<boolean> {
const FormData = await getFormDataCls();
return typeof FormData !== 'undefined' && value instanceof FormData;
}

/**
* @private
*/
Expand Down Expand Up @@ -312,17 +294,9 @@ export function applyQueryParams<
* @private
*/
export async function parseFormParam(
originalValue: number | boolean | string | Blob | ReadStream
originalValue: number | boolean | string | Blob
): Promise<string | Blob> {
let value = originalValue;
value = typeof value == 'number' || typeof value == 'boolean' ? '' + value : value;
if (
typeof originalValue === 'object' &&
'path' in originalValue &&
typeof originalValue.path === 'string'
) {
const { fileFrom } = await import('node-fetch');
value = await fileFrom(originalValue.path, 'application/json');
}
return value as string | Blob;
}
2 changes: 1 addition & 1 deletion src/management/__generated/managers/jobs-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class JobsManager extends BaseAPI {
bodyParameters: PostUsersImportsData,
initOverrides?: InitOverride
): Promise<ApiResponse<Job>> {
const formParams = new (await runtime.getFormDataCls())();
const formParams = new FormData();

if (bodyParameters.users !== undefined) {
formParams.append('users', await runtime.parseFormParam(bodyParameters.users));
Expand Down
4 changes: 1 addition & 3 deletions src/management/__generated/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { ReadStream } from 'fs';

/**
*
*/
Expand Down Expand Up @@ -11963,7 +11961,7 @@ export interface GetJobsByIdRequest {
export interface PostUsersImportsData {
/**
*/
users: Blob | ReadStream;
users: Blob;
/**
* connection_id of the connection to which users will be imported.
*
Expand Down
Loading