Skip to content

Commit 003aabc

Browse files
committed
feat: octokit.request()
1 parent ca9355d commit 003aabc

File tree

3 files changed

+133
-2
lines changed

3 files changed

+133
-2
lines changed

src/index.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { OctokitOptions, Plugin } from "./types";
1+
import getUserAgent from "universal-user-agent";
2+
import { request } from "@octokit/request";
3+
4+
import { OctokitOptions, Parameters, Plugin } from "./types";
5+
import { VERSION } from "./version";
26

37
export class Octokit {
48
static plugins: Plugin[] = [];
@@ -13,12 +17,44 @@ export class Octokit {
1317
};
1418
}
1519

16-
constructor(options?: OctokitOptions) {
20+
constructor(options: OctokitOptions = {}) {
21+
const requestDefaults: Required<Parameters> = {
22+
baseUrl: request.endpoint.DEFAULTS.baseUrl,
23+
headers: {},
24+
request: options.request || {},
25+
mediaType: {
26+
previews: [],
27+
format: ""
28+
}
29+
};
30+
31+
// prepend default user agent with `options.userAgent` if set
32+
requestDefaults.headers["user-agent"] = [
33+
options.userAgent,
34+
`octokit-core.js/${VERSION} ${getUserAgent()}`
35+
]
36+
.filter(Boolean)
37+
.join(" ");
38+
39+
if (options.baseUrl) {
40+
requestDefaults.baseUrl = options.baseUrl;
41+
}
42+
43+
if (options.previews) {
44+
requestDefaults.mediaType.previews = options.previews;
45+
}
46+
47+
this.request = request.defaults(requestDefaults);
48+
49+
// apply plugins
1750
// https://stackoverflow.com/a/16345172
1851
const classConstructor = this.constructor as typeof Octokit;
1952
classConstructor.plugins.forEach(plugin => plugin(this, options));
2053
}
2154

55+
// assigned during constructor
56+
request: typeof request;
57+
2258
// allow for plugins to extend the Octokit instance
2359
[key: string]: any;
2460
}

src/types.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,101 @@
1+
import { Agent } from "http";
2+
13
import { Octokit } from ".";
24

35
export type OctokitOptions = {
6+
request?: OctokitRequestOptions;
47
[option: string]: any;
58
};
69

710
export type Plugin = (octokit: Octokit, options?: OctokitOptions) => void;
11+
12+
// TODO: deduplicate
13+
14+
/**
15+
* Endpoint parameters
16+
*/
17+
export type Parameters = {
18+
/**
19+
* Base URL to be used when a relative URL is passed, such as `/orgs/:org`.
20+
* If `baseUrl` is `https://enterprise.acme-inc.com/api/v3`, then the request
21+
* will be sent to `https://enterprise.acme-inc.com/api/v3/orgs/:org`.
22+
*/
23+
baseUrl?: string;
24+
25+
/**
26+
* HTTP headers. Use lowercase keys.
27+
*/
28+
headers?: RequestHeaders;
29+
30+
/**
31+
* Media type options, see {@link https://developer.github.com/v3/media/|GitHub Developer Guide}
32+
*/
33+
mediaType?: {
34+
/**
35+
* `json` by default. Can be `raw`, `text`, `html`, `full`, `diff`, `patch`, `sha`, `base64`. Depending on endpoint
36+
*/
37+
format?: string;
38+
39+
/**
40+
* Custom media type names of {@link https://developer.github.com/v3/media/|API Previews} without the `-preview` suffix.
41+
* Example for single preview: `['squirrel-girl']`.
42+
* Example for multiple previews: `['squirrel-girl', 'mister-fantastic']`.
43+
*/
44+
previews?: string[];
45+
};
46+
47+
/**
48+
* Pass custom meta information for the request. The `request` object will be returned as is.
49+
*/
50+
request?: OctokitRequestOptions;
51+
52+
/**
53+
* Any additional parameter will be passed as follows
54+
* 1. URL parameter if `':parameter'` or `{parameter}` is part of `url`
55+
* 2. Query parameter if `method` is `'GET'` or `'HEAD'`
56+
* 3. Request body if `parameter` is `'data'`
57+
* 4. JSON in the request body in the form of `body[parameter]` unless `parameter` key is `'data'`
58+
*/
59+
[parameter: string]: any;
60+
};
61+
62+
export type RequestHeaders = {
63+
/**
64+
* Avoid setting `accept`, use `mediaFormat.{format|previews}` instead.
65+
*/
66+
accept?: string;
67+
/**
68+
* Use `authorization` to send authenticated request, remember `token ` / `bearer ` prefixes. Example: `token 1234567890abcdef1234567890abcdef12345678`
69+
*/
70+
authorization?: string;
71+
/**
72+
* `user-agent` is set do a default and can be overwritten as needed.
73+
*/
74+
"user-agent"?: string;
75+
76+
[header: string]: string | number | undefined;
77+
};
78+
79+
export type Fetch = any;
80+
export type Signal = any;
81+
82+
export type OctokitRequestOptions = {
83+
/**
84+
* Node only. Useful for custom proxy, certificate, or dns lookup.
85+
*/
86+
agent?: Agent;
87+
/**
88+
* Custom replacement for built-in fetch method. Useful for testing or request hooks.
89+
*/
90+
fetch?: Fetch;
91+
/**
92+
* Use an `AbortController` instance to cancel a request. In node you can only cancel streamed requests.
93+
*/
94+
signal?: Signal;
95+
/**
96+
* Node only. Request/response timeout in ms, it resets on redirect. 0 to disable (OS limit applies). `options.request.signal` is recommended instead.
97+
*/
98+
timeout?: number;
99+
100+
[option: string]: any;
101+
};

src/version.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const VERSION = "0.0.0-development";

0 commit comments

Comments
 (0)