Skip to content

Commit

Permalink
chore: Improve TypeScript definitions (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
koistya committed May 31, 2022
1 parent 6fcfc86 commit de7daf3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
41 changes: 20 additions & 21 deletions gcp/auth.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
/* SPDX-FileCopyrightText: 2020-present Kriasoft */
/* SPDX-FileCopyrightText: 2022-present Kriasoft */
/* SPDX-License-Identifier: MIT */

import QuickLRU from "quick-lru";
import { Credentials, getCredentials } from "./credentials.js";
import { importKey, sign } from "./crypto.js";

type AuthTokenOptions<T extends "accessToken" | "idToken"> =
T extends "accessToken"
? {
credentials: Credentials | string;
scope: string[] | string;
audience?: never;
}
: {
credentials: Credentials | string;
audience: string;
scope?: never;
};
type AccessTokenOptions = {
credentials: Credentials | string;
scope?: string[] | string;
};

type IdTokenOptions = {
credentials: Credentials | string;
audience: string;
};

type AccessToken = {
accessToken: string;
Expand Down Expand Up @@ -52,17 +49,19 @@ const cache = new QuickLRU<symbol, any>({
* const headers = { Authorization: `Bearer ${token.accessToken}` };
* const res = await fetch(url, { headers });
*/
async function getAuthToken<
T extends AuthTokenOptions<"accessToken"> | AuthTokenOptions<"idToken">
>(
options: T
): Promise<T extends AuthTokenOptions<"accessToken"> ? AccessToken : IdToken> {
async function getAuthToken(options: AccessTokenOptions): Promise<AccessToken>;
async function getAuthToken(options: IdTokenOptions): Promise<IdToken>;
async function getAuthToken(
options: AccessTokenOptions | IdTokenOptions
): Promise<AccessToken | IdToken> {
// Normalize input arguments
const credentials = getCredentials(options.credentials);
const scope =
"scope" in options && Array.isArray(options.scope)
? options.scope.sort().join(" ")
: (options.scope as string | undefined) ?? options.audience;
"scope" in options || !("audience" in options)
? Array.isArray(options.scope)
? options.scope.sort().join(" ")
: options.scope
: options.audience;

// Attempt to retrieve the token from the cache
const keyId = credentials?.private_key_id ?? credentials.client_email;
Expand Down
4 changes: 4 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* SPDX-FileCopyrightText: 2022-present Kriasoft */
/* SPDX-License-Identifier: MIT */

export * as gcp from "./gcp/index.js";
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@
"email": "hello@kriasoft.com",
"url": "https://github.com/kriasoft"
},
"contributors": [
{
"name": "Konstantin Tarkus",
"email": "hello@tarkus.me",
"url": "https://github.com/koistya"
}
],
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/kriasoft"
},
{
"type": "patreon",
"url": "https://www.patreon.com/koistya"
}
],
"repository": "kriasoft/web-auth-library",
"keywords": [
"auth",
Expand Down

0 comments on commit de7daf3

Please sign in to comment.