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

fix(RequestInterface): Revert method overloading with adding some tests #405

Merged
merged 6 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
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
9 changes: 8 additions & 1 deletion src/RequestInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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`.
*/
(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>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we still need the overwrite? Shouldn't we combine this with L:26-29 as it was before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh...sorry, I have overlooked #404 is not reverting 🙇‍♂️
Personally I don't know any reason of keeping overloading, so just reverted in this PR.

5ef5f42
a1b9cd5


/**
* 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"]
}