Skip to content

Commit

Permalink
feat: use built-in fetch when available
Browse files Browse the repository at this point in the history
  • Loading branch information
porcellus committed Sep 22, 2023
1 parent c8703ca commit 48767ad
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 87 deletions.
11 changes: 7 additions & 4 deletions lib/build/framework/fastify/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// @ts-nocheck
/// <reference types="node" />
export type { SessionRequest } from "./framework";
export declare const plugin: import("fastify").FastifyPluginCallback<Record<never, never>, import("http").Server>;
export declare const plugin: import("fastify").FastifyPluginCallback<
Record<never, never>,
import("fastify").RawServerDefault
>;
export declare const errorHandler: () => (
err: any,
req: import("fastify").FastifyRequest<
import("fastify/types/route").RouteGenericInterface,
import("http").Server,
import("fastify").RawServerDefault,
import("http").IncomingMessage
>,
res: import("fastify").FastifyReply<
import("http").Server,
import("fastify").RawServerDefault,
import("http").IncomingMessage,
import("http").ServerResponse,
import("http").ServerResponse<import("http").IncomingMessage>,
import("fastify/types/route").RouteGenericInterface,
unknown
>
Expand Down
2 changes: 1 addition & 1 deletion lib/build/framework/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export declare function setCookieForServerResponse(
expires: number,
path: string,
sameSite: "strict" | "lax" | "none"
): ServerResponse;
): ServerResponse<IncomingMessage>;
export declare function getCookieValueToSetInHeader(
prev: string | string[] | undefined,
val: string | string[],
Expand Down
55 changes: 9 additions & 46 deletions lib/build/querier.js

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

4 changes: 2 additions & 2 deletions lib/build/recipe/dashboard/api/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const querier_1 = require("../../../querier");
const normalisedURLPath_1 = __importDefault(require("../../../normalisedURLPath"));
const version_1 = require("../../../version");
const error_1 = __importDefault(require("../../../error"));
const cross_fetch_1 = __importDefault(require("cross-fetch"));
const utils_1 = require("../../../utils");
async function analyticsPost(_, ___, options, __) {
// If telemetry is disabled, dont send any event
if (!supertokens_1.default.getInstanceOrThrowError().telemetryEnabled) {
Expand Down Expand Up @@ -73,7 +73,7 @@ async function analyticsPost(_, ___, options, __) {
dashboardVersion,
};
try {
await cross_fetch_1.default("https://api.supertokens.com/0/st/telemetry", {
await utils_1.doFetch("https://api.supertokens.com/0/st/telemetry", {
method: "POST",
body: JSON.stringify(data),
headers: {
Expand Down
6 changes: 3 additions & 3 deletions lib/build/recipe/thirdparty/providers/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
* License for the specific language governing permissions and limitations
* under the License.
*/
const cross_fetch_1 = __importDefault(require("cross-fetch"));
const utils_1 = require("../../../utils");
const custom_1 = __importDefault(require("./custom"));
function getSupertokensUserInfoFromRawUserInfoResponseForGithub(rawUserInfoResponse) {
if (rawUserInfoResponse.fromUserInfoAPI === undefined) {
Expand Down Expand Up @@ -69,13 +69,13 @@ function Github(input) {
Accept: "application/vnd.github.v3+json",
};
const rawResponse = {};
const emailInfoResp = await cross_fetch_1.default("https://api.github.com/user/emails", { headers });
const emailInfoResp = await utils_1.doFetch("https://api.github.com/user/emails", { headers });
if (emailInfoResp.status >= 400) {
throw new Error(`Getting userInfo failed with ${emailInfoResp.status}: ${await emailInfoResp.text()}`);
}
const emailInfo = await emailInfoResp.json();
rawResponse.emails = emailInfo;
const userInfoResp = await cross_fetch_1.default("https://api.github.com/user", { headers });
const userInfoResp = await utils_1.doFetch("https://api.github.com/user", { headers });
if (userInfoResp.status >= 400) {
throw new Error(`Getting userInfo failed with ${userInfoResp.status}: ${await userInfoResp.text()}`);
}
Expand Down
3 changes: 1 addition & 2 deletions lib/build/recipe/thirdparty/providers/gitlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var __importDefault =
};
Object.defineProperty(exports, "__esModule", { value: true });
const custom_1 = __importDefault(require("./custom"));
// import fetch from "cross-fetch";
// import NormalisedURLDomain from "../../../normalisedURLDomain";
function Gitlab(input) {
if (input.config.name === undefined) {
Expand Down Expand Up @@ -96,7 +95,7 @@ exports.default = Gitlab;
// }) {
// let accessToken = accessTokenAPIResponse.access_token;
// let authHeader = `Bearer ${accessToken}`;
// let response = await fetch(baseUrl + "/api/v4/user", {
// let response = await doFetch(baseUrl + "/api/v4/user", {
// method: "get",
// headers: {
// Authorization: authHeader,
Expand Down
6 changes: 3 additions & 3 deletions lib/build/recipe/thirdparty/providers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ var __importDefault =
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.discoverOIDCEndpoints = exports.verifyIdTokenFromJWKSEndpointAndGetPayload = exports.doPostRequest = exports.doGetRequest = void 0;
const cross_fetch_1 = __importDefault(require("cross-fetch"));
const jose = __importStar(require("jose"));
const normalisedURLDomain_1 = __importDefault(require("../../../normalisedURLDomain"));
const normalisedURLPath_1 = __importDefault(require("../../../normalisedURLPath"));
const logger_1 = require("../../../logger");
const utils_1 = require("../../../utils");
async function doGetRequest(url, queryParams, headers) {
logger_1.logDebugMessage(
`GET request to ${url}, with query params ${JSON.stringify(queryParams)} and headers ${JSON.stringify(headers)}`
Expand All @@ -56,7 +56,7 @@ async function doGetRequest(url, queryParams, headers) {
}
const finalURL = new URL(url);
finalURL.search = new URLSearchParams(queryParams).toString();
let response = await cross_fetch_1.default(finalURL.toString(), {
let response = await utils_1.doFetch(finalURL.toString(), {
headers: headers,
});
if (response.status >= 400) {
Expand All @@ -80,7 +80,7 @@ async function doPostRequest(url, params, headers) {
`POST request to ${url}, with params ${JSON.stringify(params)} and headers ${JSON.stringify(headers)}`
);
const body = new URLSearchParams(params).toString();
let response = await cross_fetch_1.default(url, {
let response = await utils_1.doFetch(url, {
method: "POST",
body,
headers,
Expand Down
1 change: 1 addition & 0 deletions lib/build/utils.d.ts

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

11 changes: 9 additions & 2 deletions lib/build/utils.js

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

22 changes: 10 additions & 12 deletions lib/ts/querier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
import fetch, { Response } from "cross-fetch";

import { getLargestVersionFromIntersection } from "./utils";
import { doFetch, getLargestVersionFromIntersection } from "./utils";
import { cdiSupported } from "./version";
import NormalisedURLDomain from "./normalisedURLDomain";
import NormalisedURLPath from "./normalisedURLPath";
Expand Down Expand Up @@ -58,7 +56,7 @@ export class Querier {
"api-key": Querier.apiKey,
};
}
let response = await fetch(url, {
let response = await doFetch(url, {
method: "GET",
headers,
});
Expand Down Expand Up @@ -132,7 +130,7 @@ export class Querier {
rid: this.rIdToCore,
};
}
return fetch(url, {
return doFetch(url, {
method: "POST",
body: body !== undefined ? JSON.stringify(body) : undefined,
headers,
Expand Down Expand Up @@ -167,7 +165,7 @@ export class Querier {
const searchParams = new URLSearchParams(params);
finalURL.search = searchParams.toString();

return fetch(finalURL.toString(), {
return doFetch(finalURL.toString(), {
method: "DELETE",
body: body !== undefined ? JSON.stringify(body) : undefined,
headers,
Expand Down Expand Up @@ -206,7 +204,7 @@ export class Querier {
Object.entries(params).filter(([_, value]) => value !== undefined) as string[][]
);
finalURL.search = searchParams.toString();
return await fetch(finalURL.toString(), {
return doFetch(finalURL.toString(), {
method: "GET",
headers,
});
Expand Down Expand Up @@ -243,7 +241,7 @@ export class Querier {
Object.entries(params).filter(([_, value]) => value !== undefined) as string[][]
);
finalURL.search = searchParams.toString();
return await fetch(finalURL.toString(), {
return doFetch(finalURL.toString(), {
method: "GET",
headers,
});
Expand Down Expand Up @@ -273,7 +271,7 @@ export class Querier {
};
}

return fetch(url, {
return doFetch(url, {
method: "PUT",
body: body !== undefined ? JSON.stringify(body) : undefined,
headers,
Expand Down Expand Up @@ -350,10 +348,10 @@ export class Querier {
err.message.includes("ECONNREFUSED") ||
err.code === "ECONNREFUSED")
) {
return await this.sendRequestHelper(path, method, requestFunc, numberOfTries - 1, retryInfoMap);
return this.sendRequestHelper(path, method, requestFunc, numberOfTries - 1, retryInfoMap);
}

if (err instanceof Response) {
if ("status" in err && "text" in err) {
if (err.status === RATE_LIMIT_STATUS_CODE) {
const retriesLeft = retryInfoMap[url];

Expand All @@ -365,7 +363,7 @@ export class Querier {

await new Promise((resolve) => setTimeout(resolve, delay));

return await this.sendRequestHelper(path, method, requestFunc, numberOfTries, retryInfoMap);
return this.sendRequestHelper(path, method, requestFunc, numberOfTries, retryInfoMap);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/ts/recipe/dashboard/api/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Querier } from "../../../querier";
import NormalisedURLPath from "../../../normalisedURLPath";
import { version as SDKVersion } from "../../../version";
import STError from "../../../error";
import fetch from "cross-fetch";
import { doFetch } from "../../../utils";

export type Response = {
status: "OK";
Expand Down Expand Up @@ -85,7 +85,7 @@ export default async function analyticsPost(
};

try {
await fetch("https://api.supertokens.com/0/st/telemetry", {
await doFetch("https://api.supertokens.com/0/st/telemetry", {
method: "POST",
body: JSON.stringify(data),
headers: {
Expand Down
Loading

0 comments on commit 48767ad

Please sign in to comment.