Skip to content

Commit

Permalink
fix(RequestInterface): Revert method overloading with adding some tes…
Browse files Browse the repository at this point in the history
…ts (#405)
  • Loading branch information
kachick authored Jun 28, 2022
1 parent e070137 commit 11d6d8f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"lint": "prettier --check '{src,test,scripts}/**/*.{js,ts,json}' README.md package.json !src/generated/* !scripts/update-endpoints/generated/*",
"lint:fix": "prettier --write '{src,test,scripts}/**/*.{js,ts,json}' README.md package.json !src/generated/* !scripts/update-endpoints/generated/*",
"pretest": "npm run -s lint",
"test": "npx tsc --noEmit --declaration --noUnusedLocals src/index.ts test.ts",
"test": "npx tsc --project tsconfig.test.json",
"update-endpoints": "npm-run-all update-endpoints:*",
"update-endpoints:fetch-json": "node scripts/update-endpoints/fetch-json",
"update-endpoints:typescript": "node scripts/update-endpoints/typescript",
Expand Down
20 changes: 8 additions & 12 deletions src/RequestInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@ export interface RequestInterface<D extends object = object> {
* @param {string} route Request method + URL. Example: `'GET /orgs/{org}'`
* @param {object} [parameters] URL, query or body parameters, as well as `headers`, `mediaType.{format|previews}`, `request`, or `baseUrl`.
*/
<R extends keyof Endpoints>(
route: R,
options?: Endpoints[R]["parameters"] & RequestParameters
): Promise<Endpoints[R]["response"]>;

/**
* Sends a request based on endpoint options
*
* @param {string} route Request method + URL. Example: `'GET /orgs/{org}'`
* @param {object} [parameters] URL, query or body parameters, as well as `headers`, `mediaType.{format|previews}`, `request`, or `baseUrl`.
*/
(route: Route, options?: RequestParameters): Promise<OctokitResponse<any>>;
<R extends Route>(
route: keyof Endpoints | R,
options?: R extends keyof Endpoints
? Endpoints[R]["parameters"] & RequestParameters
: RequestParameters
): R extends keyof Endpoints
? Promise<Endpoints[R]["response"]>
: Promise<OctokitResponse<any>>;

/**
* Returns a new `request` with updated route and parameters
Expand Down
30 changes: 25 additions & 5 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// This code is not executed, only statically analyzed using `tsc --noEmit`
import { EndpointInterface, Endpoints } from "./src";
import { EndpointInterface, Endpoints, RequestInterface, RequestParameters, Route } from "./src";

const endpoint = null as EndpointInterface;
const endpoint = {} as EndpointInterface;
function assertString(type: string) {}
function assertNullableString(type: string | null | undefined) {}
function assertArray(type: unknown[]) {}
const assertPaginate = {} as {
<R extends Route>(route: R): Promise<void>;
<R extends RequestInterface>(request: R): Promise<void>;
};

const createIssueOptions = {
owner: "octocat",
Expand All @@ -26,13 +31,28 @@ const resultMerge2 = endpoint.merge(createIssueOptions);
assertString(result.headers["x-foo"]);
assertString(resultMerge.title);
assertString(resultMerge.headers["x-foo"]);
assertString(resultMerge2.url);
assertNullableString(resultMerge2.url);

const test = {} as Endpoints["GET /repos/{owner}/{repo}/issues"]["response"];
assertArray(test.data);
assertString(test.data[0].assignees[0].avatar_url);
assertString(test.data[0].assignees![0].avatar_url);
const test2 = {} as Endpoints["GET /scim/v2/organizations/{org}/Users"]["response"];
assertString(test2.data.Resources[0].name.givenName);
assertNullableString(test2.data.Resources[0].name.givenName);

const test3 = {} as Endpoints["POST /user/repos"]["parameters"];
assertString(test3.name);

const checkRunsRoute = 'GET /repos/{owner}/{repo}/commits/{ref}/check-runs' as const;

assertPaginate(checkRunsRoute);

const listForRef = {} as {
(params?: RequestParameters & Omit<Endpoints["GET /repos/{owner}/{repo}/commits/{ref}/check-runs"]["parameters"], "baseUrl" | "headers" | "mediaType">): Promise<Endpoints[typeof checkRunsRoute]["response"]>;
defaults: RequestInterface["defaults"];
endpoint: EndpointInterface<{
url: string;
}>;
};

// octokit.paginate can take `request` method. ref: https://github.com/octokit/plugin-paginate-rest.js/blob/b3fb11e301f9658554e110aeebbd7cbb89b8aad4/README.md?plain=1#L117-L126
assertPaginate(listForRef);
9 changes: 9 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": true,
"declaration": true,
"noUnusedLocals": true
},
"include": ["test.ts"]
}

0 comments on commit 11d6d8f

Please sign in to comment.