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

feat: @octokit-next/request and support for setting explicit version in octokit.request(route, { request: { version } }) #13

Merged
merged 2 commits into from
Aug 11, 2021
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
20 changes: 19 additions & 1 deletion package-lock.json

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

16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
"author": "Gregor Martynus (https://twitter.com/gr2m)",
"license": "MIT",
"repository": "github:octokit/octokit-next.js",
"dependencies": {
"node-fetch": "3.0.0-beta.10"
},
"types": "./index.d.ts",
"exports": "./index.js",
"scripts": {
Expand Down Expand Up @@ -42,13 +39,19 @@
[
"@semantic-release/npm",
{
"pkgRoot": "packages/core"
"pkgRoot": "packages/types"
}
],
[
"@semantic-release/npm",
{
"pkgRoot": "packages/types"
"pkgRoot": "packages/request"
}
],
[
"@semantic-release/npm",
{
"pkgRoot": "packages/core"
}
],
[
Expand Down Expand Up @@ -82,7 +85,8 @@
"packages/core",
"packages/types-rest-api-github.com",
"packages/types-rest-api-ghes-3.1",
"packages/types-rest-api-ghes-3.0"
"packages/types-rest-api-ghes-3.0",
"packages/request"
],
"engines": {
"node": ">=16.5.0",
Expand Down
14 changes: 11 additions & 3 deletions packages/core/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { request as coreRequest } from "./request/index.js";
import { request as coreRequest } from "@octokit-next/request";

export class Octokit {
static withPlugins(newPlugins) {
Expand Down Expand Up @@ -35,7 +35,15 @@ export class Octokit {
});
}

request(route, parameters) {
return coreRequest(this.options, route, parameters);
request(route, parameters = {}) {
const requestOptions = {
...this.options.request,
...parameters.request,
};
return coreRequest(route, {
...{ baseUrl: this.options.baseUrl },
...parameters,
request: requestOptions,
});
}
}
6 changes: 2 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
"description": "Core SDK module for upcoming Octokit",
"exports": "./index.js",
"types": "./index.d.ts",
"scripts": {
"test": "tsd"
},
"repository": {
"type": "git",
"url": "https://github.com/octokit/octokit-next.js.git",
Expand All @@ -19,6 +16,7 @@
"author": "Gregor Martynus (https://twitter.com/gr2m)",
"license": "MIT",
"dependencies": {
"@octokit-next/types": "0.0.0-development"
"@octokit-next/types": "0.0.0-development",
"@octokit-next/request": "0.0.0-development"
}
}
3 changes: 3 additions & 0 deletions packages/request/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { RequestInterface } from "@octokit-next/types";

export declare const request: RequestInterface;
28 changes: 17 additions & 11 deletions packages/core/request/index.js → packages/request/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
import nodeFetch from "node-fetch";

const DEFAULTS = {
baseURL: "https://api.github.com",
request: {
fetch: nodeFetch,
},
};

/**
* Naive implementation of [`@octokit/request`](https://github.com/octokit/request.js/) for testing purposes
*
* @param {import("@octokit-next/types").Octokit.Options} ConstructorOptions
* @param {string} route
* @param {object} [parameters] route parameters
*/
export async function request(
{ baseUrl, request: { fetch: octokitFetch } },
route,
parameters = {}
) {
export async function request(route, parameters = {}) {
const requestOptions = { ...DEFAULTS.request, ...parameters.request };

const baseUrl = parameters.baseUrl || DEFAULTS.baseURL;
const [method, pathOrUrl] = route.split(" ");
const url = new URL(pathOrUrl, baseUrl).href;

/* c8 ignore next */
const fetch = octokitFetch || nodeFetch;

const response = await fetch(url, { method, headers: parameters.headers });
const response = await requestOptions.fetch(url, {
method,
headers: parameters.headers,
});

if (!response.ok) {
const error = new Error(response.statusText);
error.response = response;
error.request = {
baseUrl,
route,
parameters,
};
throw Error;
error.response = response;
throw error;
}

return {
Expand Down
20 changes: 20 additions & 0 deletions packages/request/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expectType } from "tsd";
import { request } from "./index.js";

import "@octokit-next/types-rest-api-ghes-3.1";

export async function test() {
const rootEndpointResponse = await request("GET /");

expectType<number>(rootEndpointResponse.status);
expectType<string>(rootEndpointResponse.url);
expectType<string>(rootEndpointResponse.headers["x-ratelimit-limit"]);
expectType<string>(rootEndpointResponse.data.emojis_url);

const ghesOnlyResponse = await request("GET /ghes-only", {
request: {
version: "ghes-3.1",
},
});
expectType<boolean>(ghesOnlyResponse.data.ok);
}
22 changes: 22 additions & 0 deletions packages/request/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@octokit-next/request",
"version": "0.0.0-development",
"description": "Simplified version of `@octokit/request` to experiment with ESM and types",
"publishConfig": {
"access": "public"
},
"type": "module",
"exports": "./index.js",
"types": "./index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/octokit/octokit-next.js.git",
"directory": "packages/core"
},
"author": "Gregor Martynus (https://twitter.com/gr2m)",
"license": "MIT",
"dependencies": {
"@octokit-next/types": "0.0.0-development",
"node-fetch": "3.0.0-beta.10"
}
}
22 changes: 16 additions & 6 deletions packages/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { RequestInterface } from "./request";
export { RequestInterface } from "./request";

/**
* Global Octokit interfaces that can be extended as needed.
*/
export namespace Octokit {
interface Options {
version?: keyof Octokit.ApiVersions;

/**
* GitHub's REST API base URL. Defaults to https://api.github.com
*
Expand All @@ -19,12 +22,19 @@ export namespace Octokit {
*/
userAgent?: string;

request?: {
/**
* override the built-in fetch method, e.g. for testing
*/
fetch?: (resource: any, init?: any) => Promise<{}>;
};
request?: RequestOptions;
}

interface RequestOptions {
/**
* Override API version on a per-request basis.
*/
version?: keyof Octokit.ApiVersions;

/**
* override the built-in fetch method, e.g. for testing
*/
fetch?: (resource: any, init?: any) => Promise<any>;
}

interface ResponseHeaders {
Expand Down
4 changes: 3 additions & 1 deletion packages/types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expectType } from "tsd";
import { Octokit, Plugin } from "./index.js";

import "@octokit-next/types-rest-api-github.com";

export async function test() {
// TODO:
// new Octokit();
Expand All @@ -26,7 +28,7 @@ export async function test() {
expectType<string>(emojisResponseDotcom.data["dotcom-only"]);

// @ts-expect-error - ghes-only does not exist
emojisResponseDotcom.data["ghes-only"];
type test = typeof emojisResponseDotcom["data"]["ghes-only"];

const OctokitWithEmptyDefaults = Octokit.withDefaults({
// there should be no required options
Expand Down
Loading