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

fix(typescript): Remove node-fetch in TypeScript SDK #5476

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 1 addition & 4 deletions docker/seed/Dockerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ RUN yarn add \
# jest
[email protected] \
@types/[email protected] \
# node fetch
[email protected] \
@types/[email protected] \
# node
@types/[email protected] \
# qs
[email protected] \
@types/[email protected] \
# url join
# url join
[email protected] \
@types/[email protected] \
# readable stream
Expand Down
3 changes: 3 additions & 0 deletions generators/typescript/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.46.0] - 2024-12-27

- Feat: Remove `node-fetch` from generated SDK dependencies and always use the global fetch function instead, this lets you bundle the SDK for the browser with tools like Vite and Rollup without marking node-fetch as external.
## [0.45.1] - 2024-12-27

- Fix: Export everything inside of TypeScript namespaces that used to be ambient.
Expand Down
2 changes: 1 addition & 1 deletion generators/typescript/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.45.1
0.46.0
3 changes: 1 addition & 2 deletions generators/typescript/sdk/features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ features:
- id: RUNTIME_COMPATIBILITY
advanced: true
description: |
The SDK defaults to `node-fetch` but will use the global fetch client if present. The SDK works in the following
runtimes:
The SDK works in the following runtimes:

- Node.js 18+
- Vercel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,11 @@ export class FetcherImpl extends CoreUtility implements Fetcher {
addDependencies: (dependencyManager: DependencyManager): void => {
dependencyManager.addDependency("form-data", "^4.0.0");
dependencyManager.addDependency("formdata-node", "^6.0.3");
dependencyManager.addDependency("node-fetch", "^2.7.0");
dependencyManager.addDependency("qs", "^6.13.1");
dependencyManager.addDependency("readable-stream", "^4.5.2");
dependencyManager.addDependency("@types/qs", "^6.9.17", {
type: DependencyType.DEV
});
dependencyManager.addDependency("@types/node-fetch", "^2.6.12", {
type: DependencyType.DEV
});
dependencyManager.addDependency("@types/readable-stream", "^4.0.18", {
type: DependencyType.DEV
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { APIResponse } from "./APIResponse";
import { createRequestUrl } from "./createRequestUrl";
import { getFetchFn } from "./getFetchFn";
import { getRequestBody } from "./getRequestBody";
import { getResponseBody } from "./getResponseBody";
import { makeRequest } from "./makeRequest";
Expand Down Expand Up @@ -64,17 +63,16 @@ export async function fetcherImpl<R = unknown>(args: Fetcher.Args): Promise<APIR
}

const url = createRequestUrl(args.url, args.queryParameters);
let requestBody: BodyInit | undefined = await getRequestBody({
const requestBody: BodyInit | undefined = await getRequestBody({
body: args.body,
type: args.requestType === "json" ? "json" : "other"
});
const fetchFn = await getFetchFn();

try {
const response = await requestWithRetries(
async () =>
makeRequest(
fetchFn,
fetch,
url,
args.method,
headers,
Expand All @@ -86,7 +84,7 @@ export async function fetcherImpl<R = unknown>(args: Fetcher.Args): Promise<APIR
),
args.maxRetries
);
let responseBody = await getResponseBody(response, args.responseType);
const responseBody = await getResponseBody(response, args.responseType);

if (response.status >= 200 && response.status < 400) {
return {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface CrossPlatformFormData {

appendFile(key: string, value: unknown, fileName?: string): Promise<void>;

getRequest(): MaybePromise<FormDataRequest<unknown>>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually use unknown instead of any. Any reason why this was changed here?

Copy link
Author

@remorses remorses Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, now fetch type is more strict and no longer accepts a RequestInit<unknown> so many type checks in tests for example fail

That unknown has no reason to exist because there is no way to override it with a generic

getRequest(): MaybePromise<FormDataRequest<any>>;
}

export async function newFormData(): Promise<CrossPlatformFormData> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import fs from "fs";
import { Server } from "http";
import multer from "multer";
import { newFormData } from "../..";
import { getFetchFn } from "../../fetcher/getFetchFn";
import { RUNTIME } from "../../runtime";

describe("Multipart Form Data Tests", () => {
Expand Down Expand Up @@ -48,7 +47,6 @@ describe("Multipart Form Data Tests", () => {

await fdw.appendFile("file", new Blob([new Uint8Array(y)]), "asda");

const fetch = await getFetchFn();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main change

const response = await fetch("http://localhost:4567/upload", {
method: "POST",
...(await fdw.getRequest()),
Expand All @@ -66,8 +64,6 @@ describe("Multipart Form Data Tests", () => {
const y = fs.readFileSync("package.json");
await fdw.appendFile("file", new File([y], "package.json"));

const fetch = await getFetchFn();

const response = await fetch("http://localhost:4567/upload", {
method: "POST",
...(await fdw.getRequest()),
Expand All @@ -85,8 +81,6 @@ describe("Multipart Form Data Tests", () => {
const y = fs.createReadStream("package.json");
await fdw.appendFile("file", y);

const fetch = await getFetchFn();

const response = await fetch("http://localhost:4567/upload", {
method: "POST",
...(await fdw.getRequest()),
Expand Down
2 changes: 0 additions & 2 deletions pnpm-lock.yaml

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

Loading
Loading