Skip to content

Commit

Permalink
feat: improve TypeScript definitions via @octokit/types (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m authored Oct 25, 2019
1 parent e544ea4 commit 8507b3b
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 186 deletions.
65 changes: 28 additions & 37 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
},
"homepage": "https://github.com/octokit/auth-app.js#readme",
"dependencies": {
"@octokit/request": "^5.0.2",
"@octokit/request": "^5.3.0",
"@octokit/types": "^1.0.0",
"@types/lru-cache": "^5.1.0",
"lru-cache": "^5.1.1",
"universal-github-app-jwt": "^1.0.1",
Expand Down
4 changes: 2 additions & 2 deletions src/get-installation-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { get, set } from "./cache";
import { getAppAuthentication } from "./get-app-authentication";
import { toTokenAuthentication } from "./to-token-authentication";
import {
Request,
RequestInterface,
InstallationAuthOptions,
StrategyOptionsWithDefaults
} from "./types";

export async function getInstallationAuthentication(
state: StrategyOptionsWithDefaults,
options: InstallationAuthOptions,
customRequest?: Request
customRequest?: RequestInterface
) {
const installationId = (options.installationId ||
state.installationId) as number;
Expand Down
4 changes: 2 additions & 2 deletions src/get-oauth-authentication.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
Request,
RequestInterface,
OAuthOptions,
StrategyOptionsWithDefaults,
OAuthAccesTokenAuthentication
Expand All @@ -8,7 +8,7 @@ import {
export async function getOAuthAuthentication(
state: StrategyOptionsWithDefaults,
options: OAuthOptions,
customRequest?: Request
customRequest?: RequestInterface
): Promise<OAuthAccesTokenAuthentication> {
const request = customRequest || state.request;

Expand Down
23 changes: 13 additions & 10 deletions src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,34 @@ import { getInstallationAuthentication } from "./get-installation-authentication
import { requiresAppAuth } from "./requires-app-auth";
import {
AnyResponse,
Defaults,
Endpoint,
Parameters,
Request,
EndpointDefaults,
EndpointOptions,
RequestParameters,
RequestInterface,
Route,
State
} from "./types";

export async function hook(
state: State,
request: Request,
route: Route | Endpoint,
parameters?: Parameters
request: RequestInterface,
route: Route | EndpointOptions,
parameters?: RequestParameters
): Promise<AnyResponse> {
let endpoint: Defaults = request.endpoint.merge(route as string, parameters);
let endpoint: EndpointDefaults = request.endpoint.merge(
route as string,
parameters
);

if (requiresAppAuth(endpoint.url)) {
const { token } = await getAppAuthentication(state.id, state.privateKey);
endpoint.headers.authorization = `bearer ${token}`;

return request(endpoint as Endpoint);
return request(endpoint as EndpointOptions);
}

const { token } = await getInstallationAuthentication(state, {}, request);

endpoint.headers.authorization = `token ${token}`;
return request(endpoint as Endpoint);
return request(endpoint as EndpointOptions);
}
2 changes: 1 addition & 1 deletion src/requires-app-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ function routeMatcher(paths: string[]) {

const REGEX = routeMatcher(PATHS);

export function requiresAppAuth(url: string): Boolean {
export function requiresAppAuth(url: string | undefined): Boolean {
return !!url && REGEX.test(url);
}
144 changes: 11 additions & 133 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import * as OctokitTypes from "@octokit/types";
import LRUCache from "lru-cache";
import { request } from "@octokit/request";

export type AnyResponse = OctokitTypes.OctokitResponse<any>;
export type EndpointDefaults = OctokitTypes.EndpointDefaults;
export type EndpointOptions = OctokitTypes.EndpointOptions;
export type RequestParameters = OctokitTypes.RequestParameters;
export type Route = OctokitTypes.Route;
export type RequestInterface = OctokitTypes.RequestInterface;

export type Cache =
| LRUCache<string, string>
Expand Down Expand Up @@ -66,12 +73,12 @@ export type StrategyOptions = {
installationId?: number;
clientId?: string;
clientSecret?: string;
request?: typeof request;
request?: OctokitTypes.RequestInterface;
cache?: Cache;
};

export type StrategyOptionsWithDefaults = StrategyOptions & {
request: typeof request;
request: OctokitTypes.RequestInterface;
cache: Cache;
};

Expand Down Expand Up @@ -102,135 +109,6 @@ export type WithInstallationId = {
};

export type State = StrategyOptions & {
request: typeof request;
request: OctokitTypes.RequestInterface;
cache: Cache;
};

export type Request = typeof request;

// TODO: copied from @octokit/request, that should be dried up
import { Agent } from "http";
export type Fetch = any;
export type Signal = any;

export type Endpoint = Parameters & {
method: Method;
url: Url;
};

export type Method = "DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT";
export type Route = string;
export type Url = string;

export type Parameters = {
/**
* 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?: string;

/**
* 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?: OctokitRequestOptions;

/**
* 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;
};

export type OctokitRequestOptions = {
/**
* 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;
};

export type RequestHeaders = {
/**
* Avoid setting `accept`, use `mediaFormat.{format|previews}` 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;
};

export type ResponseHeaders = {
[header: string]: string;
};
export type OctokitResponse<T> = {
headers: ResponseHeaders;
/**
* http response code
*/
status: number;
/**
* URL of response after all redirects
*/
url: string;
/**
* This is the data you would see in https://developer.Octokit.com/v3/
*/
data: T;
};
export type AnyResponse = OctokitResponse<any>;

export type Defaults = Parameters & {
method: Method;
baseUrl: string;
headers: RequestHeaders & { accept: string; "user-agent": string };
mediaType: {
format: string;
previews: string[];
};
};

0 comments on commit 8507b3b

Please sign in to comment.