From f6d3bf7b4295c524cb6af4a64396918015a25375 Mon Sep 17 00:00:00 2001 From: Gregor Martynus Date: Sat, 2 Nov 2019 21:24:16 -0700 Subject: [PATCH] feat: better TypeScript definitions via @octokit/types v2 --- src/auth.ts | 8 ++++++-- src/get-app-authentication.ts | 7 ++++++- src/get-installation-authentication.ts | 5 +++-- src/index.ts | 8 +++++--- src/types.ts | 10 +++++++--- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/auth.ts b/src/auth.ts index 4843da235..caed25163 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,4 +1,8 @@ -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"; @@ -6,7 +10,7 @@ import { getOAuthAuthentication } from "./get-oauth-authentication"; export async function auth( state: StrategyOptionsWithDefaults, options: AuthOptions -) { +): Promise { if (options.type === "app") { return getAppAuthentication(state.id, state.privateKey); } diff --git a/src/get-app-authentication.ts b/src/get-app-authentication.ts index 34283c79b..f5890a4da 100644 --- a/src/get-app-authentication.ts +++ b/src/get-app-authentication.ts @@ -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 { const appAuthentication = await githubAppJwt({ id, privateKey }); return { diff --git a/src/get-installation-authentication.ts b/src/get-installation-authentication.ts index bbe5e7acf..e5a80f981 100644 --- a/src/get-installation-authentication.ts +++ b/src/get-installation-authentication.ts @@ -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 { const installationId = (options.installationId || state.installationId) as number; diff --git a/src/index.ts b/src/index.ts index e0fc2c752..304aefcda 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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({ @@ -23,4 +25,4 @@ export function createAppAuth(options: StrategyOptions): AuthInterface { return Object.assign(auth.bind(null, state), { hook: hook.bind(null, state) }); -} +}; diff --git a/src/types.ts b/src/types.ts index a4d3ab719..c14836428 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,12 +2,16 @@ import * as OctokitTypes from "@octokit/types"; import LRUCache from "lru-cache"; export type AnyResponse = OctokitTypes.OctokitResponse; -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 @@ -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 = {