Skip to content

Commit

Permalink
feat(api): update via SDK Studio (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and stainless-bot committed May 23, 2024
1 parent b523d40 commit 5ac8ff6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configured_endpoints: 38
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/plastic-labs%2FHoncho-7967581df14089cda98ce7bd258102d5da5ec541dc5b17aa918f96be11a2bde8.yml
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/plastic-labs%2Fhoncho-7967581df14089cda98ce7bd258102d5da5ec541dc5b17aa918f96be11a2bde8.yml
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The full API of this library can be found in [api.md](api.md).
import Honcho from 'honcho-ai';

const honcho = new Honcho({
apiKey: process.env['HONCHO_AUTH_TOKEN'], // This is the default and can be omitted
apiKey: process.env['HONCHO_API_KEY'], // This is the default and can be omitted
environment: 'demo', // defaults to 'local'
});

Expand All @@ -45,7 +45,7 @@ This library includes TypeScript definitions for all request params and response
import Honcho from 'honcho-ai';

const honcho = new Honcho({
apiKey: process.env['HONCHO_AUTH_TOKEN'], // This is the default and can be omitted
apiKey: process.env['HONCHO_API_KEY'], // This is the default and can be omitted
environment: 'demo', // defaults to 'local'
});

Expand Down
32 changes: 21 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ type Environment = keyof typeof environments;

export interface ClientOptions {
/**
* Defaults to process.env['HONCHO_AUTH_TOKEN'].
* Defaults to process.env['HONCHO_API_KEY'].
*/
apiKey?: string | undefined;
apiKey?: string | null | undefined;

/**
* Specifies the environment to use for the API.
Expand Down Expand Up @@ -87,14 +87,14 @@ export interface ClientOptions {

/** API Client for interfacing with the Honcho API. */
export class Honcho extends Core.APIClient {
apiKey: string;
apiKey: string | null;

private _options: ClientOptions;

/**
* API Client for interfacing with the Honcho API.
*
* @param {string | undefined} [opts.apiKey=process.env['HONCHO_AUTH_TOKEN'] ?? undefined]
* @param {string | null | undefined} [opts.apiKey=process.env['HONCHO_API_KEY'] ?? null]
* @param {Environment} [opts.environment=local] - Specifies the environment URL to use for the API.
* @param {string} [opts.baseURL=process.env['HONCHO_BASE_URL'] ?? http://localhost:8000] - Override the default base URL for the API.
* @param {number} [opts.timeout=1 minute] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
Expand All @@ -106,15 +106,9 @@ export class Honcho extends Core.APIClient {
*/
constructor({
baseURL = Core.readEnv('HONCHO_BASE_URL'),
apiKey = Core.readEnv('HONCHO_AUTH_TOKEN'),
apiKey = Core.readEnv('HONCHO_API_KEY') ?? null,
...opts
}: ClientOptions = {}) {
if (apiKey === undefined) {
throw new Errors.HonchoError(
"The HONCHO_AUTH_TOKEN environment variable is missing or empty; either provide it, or instantiate the Honcho client with an apiKey option, like new Honcho({ apiKey: 'My API Key' }).",
);
}

const options: ClientOptions = {
apiKey,
...opts,
Expand Down Expand Up @@ -153,7 +147,23 @@ export class Honcho extends Core.APIClient {
};
}

protected override validateHeaders(headers: Core.Headers, customHeaders: Core.Headers) {
if (this.apiKey && headers['authorization']) {
return;
}
if (customHeaders['authorization'] === null) {
return;
}

throw new Error(
'Could not resolve authentication method. Expected the apiKey to be set. Or for the "Authorization" headers to be explicitly omitted',
);
}

protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers {
if (this.apiKey == null) {
return {};
}
return { Authorization: `Bearer ${this.apiKey}` };
}

Expand Down
4 changes: 2 additions & 2 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ describe('instantiate client', () => {

test('with environment variable arguments', () => {
// set options via env var
process.env['HONCHO_AUTH_TOKEN'] = 'My API Key';
process.env['HONCHO_API_KEY'] = 'My API Key';
const client = new Honcho();
expect(client.apiKey).toBe('My API Key');
});

test('with overriden environment variable arguments', () => {
// set options via env var
process.env['HONCHO_AUTH_TOKEN'] = 'another My API Key';
process.env['HONCHO_API_KEY'] = 'another My API Key';
const client = new Honcho({ apiKey: 'My API Key' });
expect(client.apiKey).toBe('My API Key');
});
Expand Down

0 comments on commit 5ac8ff6

Please sign in to comment.