From 4f260fe146d24f4c74ac627cdcbb04c6deac66be Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Mon, 22 Mar 2021 23:51:15 -0700 Subject: [PATCH] fix(scopeToken): return token from response, not options. Also fix options type --- src/scope-token.ts | 44 ++++++++++++++++++++++------------------ test/scope-token.test.ts | 4 +++- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/scope-token.ts b/src/scope-token.ts index 3279cef..c96ca39 100644 --- a/src/scope-token.ts +++ b/src/scope-token.ts @@ -4,31 +4,35 @@ import btoa from "btoa-lite"; import { GitHubAppAuthentication } from "./types"; -type TargetOption = - | { - target: string; - } - | { - target_id: number; - }; -type RepositoriesOption = - | { - repositories?: string[]; - } - | { - repository_ids?: number[]; - }; -type Endpoint = Endpoints["POST /applications/{client_id}/token/scoped"]; - -export type ScopeTokenOptions = { +type CommonOptions = { clientType: "github-app"; clientId: string; clientSecret: string; token: string; permissions?: Endpoint["parameters"]["permissions"]; request?: RequestInterface; -} & TargetOption & - RepositoriesOption; +}; + +type TargetOption = { + target: string; +}; +type TargetIdOption = { + target_id: number; +}; +type RepositoriesOption = { + repositories?: string[]; +}; +type RepositoryIdsOption = { + repository_ids?: number[]; +}; + +type Endpoint = Endpoints["POST /applications/{client_id}/token/scoped"]; + +export type ScopeTokenOptions = + | (CommonOptions & TargetOption & RepositoriesOption) + | (CommonOptions & TargetIdOption & RepositoriesOption) + | (CommonOptions & TargetOption & RepositoryIdsOption) + | (CommonOptions & TargetIdOption & RepositoryIdsOption); export type ScopeTokenResponse = Endpoint["response"] & { authentication: GitHubAppAuthentication; @@ -62,7 +66,7 @@ export async function scopeToken( clientType, clientId, clientSecret, - token: options.token, + token: response.data.token, }; return { ...response, authentication }; diff --git a/test/scope-token.test.ts b/test/scope-token.test.ts index 94c5ab1..0a1aae2 100644 --- a/test/scope-token.test.ts +++ b/test/scope-token.test.ts @@ -11,6 +11,7 @@ describe("scopeToken()", () => { login: "octokit", id: 1, }, + token: "usertoken456", }, { headers: { @@ -55,6 +56,7 @@ describe("scopeToken()", () => { "id": 1, "login": "octokit", }, + "token": "usertoken456", } `); expect(authentication).toMatchInlineSnapshot(` @@ -62,7 +64,7 @@ describe("scopeToken()", () => { "clientId": "lv1.1234567890abcdef", "clientSecret": "1234567890abcdef12347890abcdef12345678", "clientType": "github-app", - "token": "usertoken123", + "token": "usertoken456", } `); });