diff --git a/.env.example b/.env.example index c322157..4eaa657 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,5 @@ NODE_ENV="development" +TRIBUFU_ACCESS_TOKEN="" TRIBUFU_API_KEY="" TRIBUFU_API_URL="https://api.tribufu.com" TRIBUFU_CLIENT_ID="" diff --git a/examples/bearer.js b/examples/bearer.js new file mode 100644 index 0000000..bea639e --- /dev/null +++ b/examples/bearer.js @@ -0,0 +1,15 @@ +// Copyright (c) Tribufu. All Rights Reserved. +// SPDX-License-Identifier: MIT AND Apache-2.0 + +import dotenv from "dotenv"; +import { TribufuApi } from "../build/index.mjs"; + +dotenv.config(); + +async function main() { + const accessToken = process.env["TRIBUFU_ACCESS_TOKEN"]; + const tribufu = TribufuApi.withAccessToken(accessToken); + console.log(await tribufu.getUserInfo()); +} + +main(); diff --git a/src/credentials.ts b/src/credentials.ts new file mode 100644 index 0000000..3a9a626 --- /dev/null +++ b/src/credentials.ts @@ -0,0 +1,12 @@ +// Copyright (c) Tribufu. All Rights Reserved. +// SPDX-License-Identifier: MIT + +export enum CredentialsType { + ApiKey, + Bearer, +} + +export interface Credentials { + type: CredentialsType; + value: string; +} diff --git a/src/index.ts b/src/index.ts index f841fb6..783a62d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: MIT import { Configuration } from "./runtime"; +import { Credentials, CredentialsType } from "./credentials"; import { JavaScriptRuntime } from "./node"; import { TRIBUFU_API_URL, TRIBUFU_VERSION } from "./constants"; import { TribufuGeneratedApi } from "./apis/TribufuGeneratedApi"; @@ -20,12 +21,12 @@ export * from "./singletion"; */ export class TribufuApi extends TribufuGeneratedApi { /** - * Create a TribufuApi with the given API key. + * Create a TribufuApi with the credentials. * - * @param apiKey The API key for authentication. + * @param credentials The credentials for authentication. */ - constructor(apiKey: string | null = null) { - super(TribufuApi.createConfiguration(apiKey)); + constructor(credentials: Credentials | null = null) { + super(TribufuApi.createConfiguration(credentials)); } /** @@ -46,7 +47,19 @@ export class TribufuApi extends TribufuGeneratedApi { * @returns TribufuApi */ public static withApiKey(apiKey: string): TribufuApi { - return new TribufuApi(apiKey); + return new TribufuApi({ type: CredentialsType.ApiKey, value: apiKey }); + } + + /** + * Create a TribufuApi with the given access token. + * + * - A access token give you user read and write access to the Tribufu API. + * + * @param accessToken + * @returns TribufuApi + */ + public static withAccessToken(accessToken: string): TribufuApi { + return new TribufuApi({ type: CredentialsType.Bearer, value: accessToken }); } /** @@ -96,14 +109,21 @@ export class TribufuApi extends TribufuGeneratedApi { /** * Creates a configuration for the Tribufu API client. */ - private static createConfiguration(apiKey: string | null): Configuration { + private static createConfiguration(credentials: Credentials | null): Configuration { const basePath = this.getBaseUrl(); const headers: Record = { "User-Agent": this.getUserAgent(), }; - if (apiKey) { - headers["Authorization"] = `ApiKey ${apiKey}`; + if (credentials) { + switch (credentials.type) { + case CredentialsType.ApiKey: + headers["Authorization"] = `ApiKey ${credentials.value}`; + break; + case CredentialsType.Bearer: + headers["Authorization"] = `Bearer ${credentials.value}`; + break; + } } return new Configuration({