Skip to content
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,15 @@
},
"dependencies": {
"@himenon/openapi-parameter-formatter": "^0.2.2",
"node-fetch": "2"
"node-fetch": "2.6.7"
},
"devDependencies": {
"@commitlint/cli": "16.2.1",
"@commitlint/config-conventional": "16.2.1",
"@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",
Expand Down
10 changes: 8 additions & 2 deletions src/ApiClientImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ export const generateQueryString = (queryParameters: QueryParameters | undefined
return queries.join("&");
};

export const create = (accessToken?: string): ApiClient<Types.RequestOption> => {
export interface Params {
accessToken?: string;
fetch: Types.FetchFunction;
}

export const create = (params: Params): ApiClient<Types.RequestOption> => {
const { accessToken, fetch: _fetch } = params;
const apiClientImpl: ApiClient<Types.RequestOption> = {
request: async (httpMethod, url, headers, requestBody, queryParameters): Promise<any> => {
const query = generateQueryString(queryParameters);
Expand All @@ -32,7 +38,7 @@ export const create = (accessToken?: string): ApiClient<Types.RequestOption> =>
...headers,
Authorization: "token " + accessToken,
};
const response = await fetch(requestUrl, {
const response = await _fetch(requestUrl, {
body: JSON.stringify(requestBody),
headers: requestHeaders,
method: httpMethod,
Expand Down
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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 };
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { RequestInfo, RequestInit, Response } from "node-fetch";

export interface RequestOption {}

export type FetchFunction = (url: RequestInfo, init?: RequestInit) => Promise<Response>;

export interface CreateBranchArgs {
branchName: string;
baseBranchName?: string;
Expand Down
10 changes: 9 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down