diff --git a/package.json b/package.json index 8223383..0981960 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,7 @@ }, "dependencies": { "@himenon/openapi-parameter-formatter": "^0.2.2", - "node-fetch": "2" + "node-fetch": "2.6.7" }, "devDependencies": { "@commitlint/cli": "16.2.1", @@ -100,6 +100,7 @@ "@himenon/openapi-typescript-code-generator": "^0.16.0", "@types/jest": "27.4.1", "@types/node": "17.0.21", + "@types/node-fetch": "^2.6.1", "@types/rimraf": "3.0.2", "@typescript-eslint/eslint-plugin": "5.14.0", "@typescript-eslint/parser": "5.14.0", diff --git a/src/ApiClientImpl.ts b/src/ApiClientImpl.ts index 530f0a5..d79f22a 100644 --- a/src/ApiClientImpl.ts +++ b/src/ApiClientImpl.ts @@ -23,7 +23,13 @@ export const generateQueryString = (queryParameters: QueryParameters | undefined return queries.join("&"); }; -export const create = (accessToken?: string): ApiClient => { +export interface Params { + accessToken?: string; + fetch: Types.FetchFunction; +} + +export const create = (params: Params): ApiClient => { + const { accessToken, fetch: _fetch } = params; const apiClientImpl: ApiClient = { request: async (httpMethod, url, headers, requestBody, queryParameters): Promise => { const query = generateQueryString(queryParameters); @@ -32,7 +38,7 @@ export const create = (accessToken?: string): ApiClient => ...headers, Authorization: "token " + accessToken, }; - const response = await fetch(requestUrl, { + const response = await _fetch(requestUrl, { body: JSON.stringify(requestBody), headers: requestHeaders, method: httpMethod, diff --git a/src/index.ts b/src/index.ts index 3e1582d..425db42 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,8 @@ import { GitHub } from "./GitHub"; import * as ApiClientImpl from "./ApiClientImpl"; import { Client, Schemas } from "./api"; +import { FetchFunction } from "./types"; +import nodeFetch from "node-fetch"; export * from "./types"; export { Schemas, GitHub, Client }; @@ -15,11 +17,19 @@ export interface InitializeParameter { baseUrl?: string | "https://api.github.com"; /** GitHub Access Token */ accessToken?: string; + /** + * Default: node-fetch + */ + fetch?: FetchFunction; } export const create = (args: InitializeParameter): GitHub => { const baseUrl = args.baseUrl || "https://api.github.com"; - const apiClientImpl = ApiClientImpl.create(args.accessToken); + const _fetch: FetchFunction = args.fetch || nodeFetch; + const apiClientImpl = ApiClientImpl.create({ + accessToken: args.accessToken, + fetch: _fetch, + }); const client = new Client(apiClientImpl, baseUrl); const github = new GitHub(client, args.owner, args.repo); return github; diff --git a/src/types.ts b/src/types.ts index 8fbac35..d6ce4ee 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,9 @@ +import type { RequestInfo, RequestInit, Response } from "node-fetch"; + export interface RequestOption {} +export type FetchFunction = (url: RequestInfo, init?: RequestInit) => Promise; + export interface CreateBranchArgs { branchName: string; baseBranchName?: string; diff --git a/yarn.lock b/yarn.lock index f70b0e3..a86b4c1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2078,6 +2078,14 @@ resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.1.tgz#283f669ff76d7b8260df8ab7a4262cc83d988256" integrity sha512-fZQQafSREFyuZcdWFAExYjBiCL7AUCdgsk80iO0q4yihYYdcIiH28CcuPTGFgLOCC8RlW49GSQxdHwZP+I7CNg== +"@types/node-fetch@^2.6.1": + version "2.6.1" + resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.1.tgz#8f127c50481db65886800ef496f20bbf15518975" + integrity sha512-oMqjURCaxoSIsHSr1E47QHzbmzNR5rK8McHuNb11BOM9cHcIK3Avy0s/b2JlXHoQGTYS3NsvWzV1M0iK7l0wbA== + dependencies: + "@types/node" "*" + form-data "^3.0.0" + "@types/node@*": version "14.14.7" resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.7.tgz#8ea1e8f8eae2430cf440564b98c6dfce1ec5945d" @@ -6406,7 +6414,7 @@ nice-try@^1.0.4: resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366" integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ== -node-fetch@2: +node-fetch@2.6.7: version "2.6.7" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad" integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==