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

feat(fetch): bring your own fetch #19

Merged
merged 1 commit into from
Dec 27, 2024
Merged
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
feat(fetch): bring your own fetch
sibelius committed Dec 27, 2024
commit e9d390b065c650b7d3bd349a6cd510e8c38a38e4
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -13,12 +13,10 @@
"@rollup/plugin-typescript": "^12.1.2",
"@sentry/node": "^8.2.1",
"@types/node": "^22.10.2",
"@types/node-fetch": "^2.6.11",
"@types/write-file-atomic": "^4.0.3",
"esbuild": "^0.24.2",
"esbuild-register": "^3.5.0",
"isomorphic-fetch": "^3.0.0",
"node-fetch": "^3.3.2",
"rollup": "^4.18.0",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-esbuild": "^6.1.1",
102 changes: 0 additions & 102 deletions pnpm-lock.yaml

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

8 changes: 3 additions & 5 deletions src/apiCache.ts
Original file line number Diff line number Diff line change
@@ -3,8 +3,6 @@ import path from 'path';
import util from 'util';
import writeFileAtomicCallback from 'write-file-atomic';

import { type RequestInfo, type RequestInit, Response } from 'node-fetch';

import { cloneResponse } from './cloneResponse.ts';
import { debugConsole } from './debugConsole.ts';
import { getCurl } from './getCurl.ts';
@@ -17,7 +15,7 @@ const cwd = process.cwd();
const dirPath = 'mock-requests.json';
const output = path.join(cwd, dirPath);

export const getRequestKey = (init: RequestInfo, options: RequestInit) => {
export const getRequestKey = (init: string | URL | globalThis.Request, options: RequestInit) => {
if (!options?.body) {
return `${options.method}:${init}`;
}
@@ -26,7 +24,7 @@ export const getRequestKey = (init: RequestInfo, options: RequestInit) => {
};

export const saveRequestMock = async (
init: RequestInfo,
init: string | URL | globalThis.Request,
options: RequestInit,
text: string,
response: Response,
@@ -89,7 +87,7 @@ export const saveRequestMock = async (
};

export const getRequestMock = async (
init: RequestInfo,
init: string | URL | globalThis.Request,
options: RequestInit,
) => {
if (process.env.USE_MOCK !== 'true') {
4 changes: 1 addition & 3 deletions src/apiDebug.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import chalk from 'chalk';

import { type RequestInfo, type RequestInit, Response } from 'node-fetch';

import { getCurl } from './getCurl.ts';
import { debugConsole } from './debugConsole.ts';
import { ignoredHeaders } from './logSecurity.ts';

type ApiDebug = {
init: RequestInfo;
init: string | URL | globalThis.Request;
options: RequestInit;
durationTime: number;
getBody: () => Record<string, string>;
3 changes: 1 addition & 2 deletions src/apiReport.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as Sentry from '@sentry/node';

import { type RequestInfo, type RequestInit, Response } from 'node-fetch';
import prettyFormat from 'pretty-format';

import { getCurl } from './getCurl.ts';
@@ -15,7 +14,7 @@ type RequestOptions = RequestInit & {
};

type ApiReport = {
init: RequestInfo;
init: string | URL | globalThis.Request;
options: RequestOptions;
// durationTime: number;
getBody: () => Record<string, string>;
7 changes: 1 addition & 6 deletions src/apiWithLog.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { type RequestInfo, type RequestInit, Response } from 'node-fetch';

import { cloneResponse } from './cloneResponse.ts';
import { timeSpan } from './timeSpan.ts';
import { apiDebug }from './apiDebug.ts';
import { apiReport } from './apiReport.ts';
import { getRequestMock, saveRequestMock } from './apiCache.ts';

const fetch = (url: URL | RequestInfo, init?: RequestInit) =>
import('node-fetch').then(({ default: fetch }) => fetch(url, init));

type RequestOptions = RequestInit & {
shouldReport?: boolean;
};

export const apiWithLog = async (
init: RequestInfo,
init: string | URL | globalThis.Request,
optionsApi: RequestOptions = { method: 'GET' },
): Promise<Response> => {
const end = timeSpan();
2 changes: 0 additions & 2 deletions src/cloneResponse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Response } from "node-fetch";

// This function clone the response consume your body and return the same response
// `clone()` is broken in `node-fetch` and results in a stalled Promise
// for responses above a certain size threshold. So construct a similar
14 changes: 6 additions & 8 deletions src/getCurl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Headers, Request, type RequestInfo, type RequestInit } from "node-fetch";

// find a b
export const ignoredHeaders = [
'host',
@@ -146,23 +144,23 @@ export function generateCompress(isEncode: boolean): string {
*
*
* @export
* @param {string|object} requestInfo
* @param {string|object} init
* @param {object={}} requestInit
*/
export const getCurl = (requestInfo: RequestInfo, requestInit: RequestInit) => {
export const getCurl = (init: Request, requestInit: RequestInit) => {
let url;
let options;

/**
* initialization with an empty object is done here to
* keep everything backwards compatible to 0.4.0 and below
*/
if (typeof requestInfo === 'string') {
url = requestInfo;
if (typeof init === 'string') {
url = init;
options = requestInit || {};
} else {
url = (requestInfo || {}).url;
options = requestInfo || {};
url = (init || {}).url;
options = init || {};
}

const { body } = options;
2 changes: 0 additions & 2 deletions src/jsonOrText.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Response } from "node-fetch";

export const jsonOrText = async (
response: Response,
): Promise<string | Record<string, unknown>> => {