Skip to content

Commit

Permalink
feat: better TypeScript definitions via @octokit/types v2
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Nov 3, 2019
1 parent 80bb1a1 commit f6d3bf7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { AuthOptions, StrategyOptionsWithDefaults } from "./types";
import {
AuthOptions,
StrategyOptionsWithDefaults,
Authentication
} from "./types";
import { getAppAuthentication } from "./get-app-authentication";
import { getInstallationAuthentication } from "./get-installation-authentication";
import { getOAuthAuthentication } from "./get-oauth-authentication";

export async function auth(
state: StrategyOptionsWithDefaults,
options: AuthOptions
) {
): Promise<Authentication> {
if (options.type === "app") {
return getAppAuthentication(state.id, state.privateKey);
}
Expand Down
7 changes: 6 additions & 1 deletion src/get-app-authentication.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { githubAppJwt } from "universal-github-app-jwt";

export async function getAppAuthentication(id: number, privateKey: string) {
import { AppAuthentication } from "./types";

export async function getAppAuthentication(
id: number,
privateKey: string
): Promise<AppAuthentication> {
const appAuthentication = await githubAppJwt({ id, privateKey });

return {
Expand Down
5 changes: 3 additions & 2 deletions src/get-installation-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { toTokenAuthentication } from "./to-token-authentication";
import {
RequestInterface,
InstallationAuthOptions,
StrategyOptionsWithDefaults
StrategyOptionsWithDefaults,
InstallationAccessTokenAuthentication
} from "./types";

export async function getInstallationAuthentication(
state: StrategyOptionsWithDefaults,
options: InstallationAuthOptions,
customRequest?: RequestInterface
) {
): Promise<InstallationAccessTokenAuthentication> {
const installationId = (options.installationId ||
state.installationId) as number;

Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { request } from "@octokit/request";
import { auth } from "./auth";
import { hook } from "./hook";
import { getCache } from "./cache";
import { AuthInterface, State, StrategyOptions } from "./types";
import { StrategyInterface, State, StrategyOptions } from "./types";
import { VERSION } from "./version";

export function createAppAuth(options: StrategyOptions): AuthInterface {
export const createAppAuth: StrategyInterface = function createAppAuth(
options: StrategyOptions
) {
const state: State = Object.assign(
{
request: request.defaults({
Expand All @@ -23,4 +25,4 @@ export function createAppAuth(options: StrategyOptions): AuthInterface {
return Object.assign(auth.bind(null, state), {
hook: hook.bind(null, state)
});
}
};
10 changes: 7 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import * as OctokitTypes from "@octokit/types";
import LRUCache from "lru-cache";

export type AnyResponse = OctokitTypes.OctokitResponse<any>;
export type AuthInterface = OctokitTypes.AuthInterface;
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 StrategyInterface = OctokitTypes.StrategyInterface<
[StrategyOptions],
[AuthOptions],
Authentication
>;

export type Cache =
| LRUCache<string, string>
Expand All @@ -33,11 +37,11 @@ export type JWT = string;
export type ACCESS_TOKEN = string;
export type UTC_TIMESTAMP = string;

type AppAuthentication = {
export type AppAuthentication = {
type: APP_TYPE;
token: JWT;
appId: number;
exporation: number;
expiresAt: string;
};

export type InstallationAccessTokenData = {
Expand Down

0 comments on commit f6d3bf7

Please sign in to comment.