Skip to content

Commit

Permalink
feat: initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Oct 24, 2019
1 parent 4237f9d commit a627077
Show file tree
Hide file tree
Showing 19 changed files with 14,553 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/EndpointDefaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { RequestHeaders } from "./RequestHeaders";
import { RequestMethod } from "./RequestMethod";
import { RequestParameters } from "./RequestParameters";

This comment was marked as off-topic.

Copy link
@jen801

jen801 Feb 22, 2022

{request method}

import { Url } from "./Url";

/**
* The `.endpoint()` method is guaranteed to set all keys defined by RequestParameters
* as well as the method property.
*/
export type EndpointDefaults = RequestParameters & {
baseUrl: Url;
method: RequestMethod;
url?: Url;
headers: RequestHeaders & {
accept: string;
"user-agent": string;
};
mediaType: {
format: string;
previews: string[];
};
};
74 changes: 74 additions & 0 deletions src/EndpointInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { EndpointDefaults } from "./EndpointDefaults";
import { EndpointOptions } from "./EndpointOptions";
import { RequestOptions } from "./RequestOptions";
import { RequestParameters } from "./RequestParameters";
import { Route } from "./Route";

import { Endpoints } from "./generated/Endpoints";

export interface EndpointInterface {
/**
* Transforms a GitHub REST API endpoint into generic request options
*
* @param {object} endpoint Must set `method` and `url`. Plus URL, query or body parameters, as well as `headers`, `mediaType.{format|previews}`, `request`, or `baseUrl`.
*/
(options: EndpointOptions): RequestOptions;

/**
* Transforms a GitHub REST API endpoint into generic request 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`.
*/
<R extends Route>(
route: keyof Endpoints | R,
options?: R extends keyof Endpoints
? Endpoints[R][0] & RequestParameters
: RequestParameters
): R extends keyof Endpoints ? Endpoints[R][1] : RequestOptions;

/**
* Object with current default route and parameters
*/
DEFAULTS: EndpointDefaults;

/**
* Returns a new `endpoint` with updated route and parameters
*/
defaults: (newDefaults: RequestParameters) => EndpointInterface;

merge: {
/**
* Merges current endpoint defaults with passed route and parameters,
* without transforming them into request 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, parameters?: RequestParameters): EndpointDefaults;

/**
* Merges current endpoint defaults with passed route and parameters,
* without transforming them into request options.
*
* @param {object} endpoint Must set `method` and `url`. Plus URL, query or body parameters, as well as `headers`, `mediaType.{format|previews}`, `request`, or `baseUrl`.
*/
(options: RequestParameters): EndpointDefaults;

/**
* Returns current default options.
*
* @deprecated use endpoint.DEFAULTS instead
*/
(): EndpointDefaults;
};

/**
* Stateless method to turn endpoint options into request options.
* Calling `endpoint(options)` is the same as calling `endpoint.parse(endpoint.merge(options))`.
*
* @param {object} options `method`, `url`. Plus URL, query or body parameters, as well as `headers`, `mediaType.{format|previews}`, `request`, or `baseUrl`.
*/
parse: (options: EndpointDefaults) => RequestOptions;
}
8 changes: 8 additions & 0 deletions src/EndpointOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RequestMethod } from "./RequestMethod";
import { Url } from "./Url";
import { RequestParameters } from "./RequestParameters";

export type EndpointOptions = RequestParameters & {
method: RequestMethod;
url: Url;
};
4 changes: 4 additions & 0 deletions src/Fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Browser's fetch method (or compatible such as fetch-mock)
*/
export type Fetch = any;
18 changes: 18 additions & 0 deletions src/OctokitResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ResponseHeaders } from "./ResponseHeaders";
import { Url } from "./Url";

export type OctokitResponse<T> = {
headers: ResponseHeaders;
/**
* http response code
*/
status: number;
/**
* URL of response after all redirects
*/
url: Url;
/**
* This is the data you would see in https://developer.Octokit.com/v3/
*/
data: T;
};
15 changes: 15 additions & 0 deletions src/RequestHeaders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export type RequestHeaders = {
/**
* Avoid setting `headers.accept`, use `mediaType.{format|previews}` option instead.
*/
accept?: string;
/**
* Use `authorization` to send authenticated request, remember `token ` / `bearer ` prefixes. Example: `token 1234567890abcdef1234567890abcdef12345678`
*/
authorization?: string;
/**
* `user-agent` is set do a default and can be overwritten as needed.
*/
"user-agent"?: string;
[header: string]: string | number | undefined;
};
51 changes: 51 additions & 0 deletions src/RequestInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { EndpointInterface } from "./EndpointInterface";
import { EndpointOptions } from "./EndpointOptions";
import { RequestParameters } from "./RequestParameters";
import { ResponseHeaders } from "./ResponseHeaders";
import { Route } from "./Route";
import { Url } from "./Url";

export interface RequestInterface {
/**
* Sends a request based on endpoint options
*
* @param {object} endpoint Must set `method` and `url`. Plus URL, query or body parameters, as well as `headers`, `mediaType.{format|previews}`, `request`, or `baseUrl`.
*/
<T = any>(options: EndpointOptions): Promise<OctokitResponse<T>>;

/**
* 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`.
*/
<T = any>(route: Route, parameters?: RequestParameters): Promise<
OctokitResponse<T>
>;

/**
* Returns a new `endpoint` with updated route and parameters
*/
defaults: (newDefaults: RequestParameters) => RequestInterface;

/**
* Octokit endpoint API, see {@link https://github.com/octokit/endpoint.js|@octokit/endpoint}
*/
endpoint: EndpointInterface;
}

export type OctokitResponse<T> = {
headers: ResponseHeaders;
/**
* http response code
*/
status: number;
/**
* URL of response after all redirects
*/
url: Url;
/**
* This is the data you would see in https://developer.Octokit.com/v3/
*/
data: T;
};
10 changes: 10 additions & 0 deletions src/RequestMethod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* HTTP Verb supported by GitHub's REST API
*/
export type RequestMethod =
| "DELETE"
| "GET"
| "HEAD"
| "PATCH"
| "POST"
| "PUT";
15 changes: 15 additions & 0 deletions src/RequestOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { RequestHeaders } from "./RequestHeaders";
import { RequestMethod } from "./RequestMethod";
import { RequestRequestOptions } from "./RequestRequestOptions";
import { Url } from "./Url";

/**
* Generic request options as they are returned by the `endpoint()` method
*/
export type RequestOptions = {
method: RequestMethod;
url: Url;
headers: RequestHeaders;
body?: any;
request?: RequestRequestOptions;
};
46 changes: 46 additions & 0 deletions src/RequestParameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { RequestRequestOptions } from "./RequestRequestOptions";
import { RequestHeaders } from "./RequestHeaders";
import { Url } from "./Url";

/**
* Parameters that can be passed into `request(route, parameters)` or `endpoint(route, parameters)` methods
*/
export type RequestParameters = {
/**
* Base URL to be used when a relative URL is passed, such as `/orgs/:org`.
* If `baseUrl` is `https://enterprise.acme-inc.com/api/v3`, then the request
* will be sent to `https://enterprise.acme-inc.com/api/v3/orgs/:org`.
*/
baseUrl?: Url;
/**
* HTTP headers. Use lowercase keys.
*/
headers?: RequestHeaders;
/**
* Media type options, see {@link https://developer.github.com/v3/media/|GitHub Developer Guide}
*/
mediaType?: {
/**
* `json` by default. Can be `raw`, `text`, `html`, `full`, `diff`, `patch`, `sha`, `base64`. Depending on endpoint
*/
format?: string;
/**
* Custom media type names of {@link https://developer.github.com/v3/media/|API Previews} without the `-preview` suffix.
* Example for single preview: `['squirrel-girl']`.
* Example for multiple previews: `['squirrel-girl', 'mister-fantastic']`.
*/
previews?: string[];
};
/**
* Pass custom meta information for the request. The `request` object will be returned as is.
*/
request?: RequestRequestOptions;
/**
* Any additional parameter will be passed as follows
* 1. URL parameter if `':parameter'` or `{parameter}` is part of `url`
* 2. Query parameter if `method` is `'GET'` or `'HEAD'`
* 3. Request body if `parameter` is `'data'`
* 4. JSON in the request body in the form of `body[parameter]` unless `parameter` key is `'data'`
*/
[parameter: string]: any;
};
27 changes: 27 additions & 0 deletions src/RequestRequestOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Agent } from "http";
import { Fetch } from "./Fetch";
import { Signal } from "./Signal";

/**
* Octokit-specific request options which are ignored for the actual request, but can be used by Octokit or plugins to manipulate how the request is sent or how a response is handled
*/
export type RequestRequestOptions = {
/**
* Node only. Useful for custom proxy, certificate, or dns lookup.
*/
agent?: Agent;
/**
* Custom replacement for built-in fetch method. Useful for testing or request hooks.
*/
fetch?: Fetch;
/**
* Use an `AbortController` instance to cancel a request. In node you can only cancel streamed requests.
*/
signal?: Signal;
/**
* Node only. Request/response timeout in ms, it resets on redirect. 0 to disable (OS limit applies). `options.request.signal` is recommended instead.
*/
timeout?: number;

[option: string]: any;
};
21 changes: 21 additions & 0 deletions src/ResponseHeaders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export type ResponseHeaders = {
"cache-control"?: string;
"content-length"?: number;
"content-type"?: string;
date?: string;
etag?: string;
"last-modified"?: string;
link?: string;
location?: string;
server?: string;
status?: string;
vary?: string;
"x-github-mediatype"?: string;
"x-github-request-id"?: string;
"x-oauth-scopes"?: string;
"x-ratelimit-limit"?: string;
"x-ratelimit-remaining"?: string;
"x-ratelimit-reset"?: string;

[header: string]: string | number | undefined;
};
4 changes: 4 additions & 0 deletions src/Route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* String consisting of an optional HTTP method and relative path or absolute URL. Examples: `'/orgs/:org'`, `'PUT /orgs/:org'`, `GET https://example.com/foo/bar`
*/
export type Route = string;
6 changes: 6 additions & 0 deletions src/Signal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Abort signal
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
*/
export type Signal = any;
4 changes: 4 additions & 0 deletions src/Url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Relative or absolute URL. Examples: `'/orgs/:org'`, `https://example.com/foo/bar`
*/
export type Url = string;
1 change: 1 addition & 0 deletions src/VERSION.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const VERSION = "0.0.0-development";
Loading

0 comments on commit a627077

Please sign in to comment.