|
4 | 4 |
|
5 | 5 | import * as environments from "../../../../environments.js"; |
6 | 6 | import * as core from "../../../../core/index.js"; |
7 | | -import * as phenoml from "../../../index.js"; |
8 | | -import { mergeHeaders, mergeOnlyDefinedHeaders } from "../../../../core/headers.js"; |
9 | | -import * as errors from "../../../../errors/index.js"; |
| 7 | +import { Auth } from "../resources/auth/client/Client.js"; |
10 | 8 |
|
11 | 9 | export declare namespace Authtoken { |
12 | 10 | export interface Options { |
13 | 11 | environment?: core.Supplier<environments.phenomlEnvironment | string>; |
14 | 12 | /** Specify a custom URL to connect the client to. */ |
15 | 13 | baseUrl?: core.Supplier<string>; |
16 | | - token: core.Supplier<core.BearerToken>; |
| 14 | + username: core.Supplier<string>; |
| 15 | + password: core.Supplier<string>; |
17 | 16 | /** Additional headers to include in requests. */ |
18 | 17 | headers?: Record<string, string | core.Supplier<string | undefined> | undefined>; |
19 | 18 | fetcher?: core.FetchFunction; |
20 | 19 | } |
21 | | - |
22 | | - export interface RequestOptions { |
23 | | - /** The maximum time to wait for a response in seconds. */ |
24 | | - timeoutInSeconds?: number; |
25 | | - /** The number of times to retry the request. Defaults to 2. */ |
26 | | - maxRetries?: number; |
27 | | - /** A hook to abort the request. */ |
28 | | - abortSignal?: AbortSignal; |
29 | | - /** Additional query string parameters to include in the request. */ |
30 | | - queryParams?: Record<string, unknown>; |
31 | | - /** Additional headers to include in the request. */ |
32 | | - headers?: Record<string, string | core.Supplier<string | undefined> | undefined>; |
33 | | - } |
34 | 20 | } |
35 | 21 |
|
36 | 22 | export class Authtoken { |
37 | 23 | protected readonly _options: Authtoken.Options; |
| 24 | + protected _auth: Auth | undefined; |
38 | 25 |
|
39 | 26 | constructor(_options: Authtoken.Options) { |
40 | 27 | this._options = _options; |
41 | 28 | } |
42 | 29 |
|
43 | | - /** |
44 | | - * Generates a JWT token using Basic Authentication with username or email and password. |
45 | | - * |
46 | | - * @param {Authtoken.RequestOptions} requestOptions - Request-specific configuration. |
47 | | - * |
48 | | - * @throws {@link phenoml.authtoken.BadRequestError} |
49 | | - * @throws {@link phenoml.authtoken.UnauthorizedError} |
50 | | - * |
51 | | - * @example |
52 | | - * await client.authtoken.generateToken() |
53 | | - */ |
54 | | - public generateToken( |
55 | | - requestOptions?: Authtoken.RequestOptions, |
56 | | - ): core.HttpResponsePromise<phenoml.authtoken.AuthtokenGenerateTokenResponse> { |
57 | | - return core.HttpResponsePromise.fromPromise(this.__generateToken(requestOptions)); |
58 | | - } |
59 | | - |
60 | | - private async __generateToken( |
61 | | - requestOptions?: Authtoken.RequestOptions, |
62 | | - ): Promise<core.WithRawResponse<phenoml.authtoken.AuthtokenGenerateTokenResponse>> { |
63 | | - const _response = await (this._options.fetcher ?? core.fetcher)({ |
64 | | - url: core.url.join( |
65 | | - (await core.Supplier.get(this._options.baseUrl)) ?? |
66 | | - (await core.Supplier.get(this._options.environment)) ?? |
67 | | - environments.phenomlEnvironment.Default, |
68 | | - "auth/token", |
69 | | - ), |
70 | | - method: "POST", |
71 | | - headers: mergeHeaders( |
72 | | - this._options?.headers, |
73 | | - mergeOnlyDefinedHeaders({ Authorization: await this._getAuthorizationHeader() }), |
74 | | - requestOptions?.headers, |
75 | | - ), |
76 | | - queryParameters: requestOptions?.queryParams, |
77 | | - timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000, |
78 | | - maxRetries: requestOptions?.maxRetries, |
79 | | - abortSignal: requestOptions?.abortSignal, |
80 | | - }); |
81 | | - if (_response.ok) { |
82 | | - return { |
83 | | - data: _response.body as phenoml.authtoken.AuthtokenGenerateTokenResponse, |
84 | | - rawResponse: _response.rawResponse, |
85 | | - }; |
86 | | - } |
87 | | - |
88 | | - if (_response.error.reason === "status-code") { |
89 | | - switch (_response.error.statusCode) { |
90 | | - case 400: |
91 | | - throw new phenoml.authtoken.BadRequestError(_response.error.body as unknown, _response.rawResponse); |
92 | | - case 401: |
93 | | - throw new phenoml.authtoken.UnauthorizedError( |
94 | | - _response.error.body as unknown, |
95 | | - _response.rawResponse, |
96 | | - ); |
97 | | - default: |
98 | | - throw new errors.phenomlError({ |
99 | | - statusCode: _response.error.statusCode, |
100 | | - body: _response.error.body, |
101 | | - rawResponse: _response.rawResponse, |
102 | | - }); |
103 | | - } |
104 | | - } |
105 | | - |
106 | | - switch (_response.error.reason) { |
107 | | - case "non-json": |
108 | | - throw new errors.phenomlError({ |
109 | | - statusCode: _response.error.statusCode, |
110 | | - body: _response.error.rawBody, |
111 | | - rawResponse: _response.rawResponse, |
112 | | - }); |
113 | | - case "timeout": |
114 | | - throw new errors.phenomlTimeoutError("Timeout exceeded when calling POST /auth/token."); |
115 | | - case "unknown": |
116 | | - throw new errors.phenomlError({ |
117 | | - message: _response.error.errorMessage, |
118 | | - rawResponse: _response.rawResponse, |
119 | | - }); |
120 | | - } |
121 | | - } |
122 | | - |
123 | | - protected async _getAuthorizationHeader(): Promise<string> { |
124 | | - return `Bearer ${await core.Supplier.get(this._options.token)}`; |
| 30 | + public get auth(): Auth { |
| 31 | + return (this._auth ??= new Auth(this._options)); |
125 | 32 | } |
126 | 33 | } |
0 commit comments