/** * Swagger Petstore - OpenAPI 3.0Lib * * This file was automatically generated by APIMATIC v3.0 ( https://www.apimatic.io ). */ import { compositeAuthenticationProvider, customHeaderAuthenticationProvider, OAuthConfiguration, requestAuthenticationProvider, } from './authentication.js'; import { Configuration } from './configuration.js'; import { OauthToken } from './models/oauthToken.js'; import { PetstoreAuthManager } from './petstoreAuthManager.js'; export function createAuthProviderFromConfig( config: Partial, petstoreAuth: () => PetstoreAuthManager | undefined ) { const authConfig = { petstoreAuth: config.petstoreAuthCredentials && requestAuthenticationProvider( config.petstoreAuthCredentials.oauthToken, petstoreAuthTokenProvider( petstoreAuth, config.petstoreAuthCredentials.oauthTokenProvider ), config.petstoreAuthCredentials.oauthOnTokenUpdate, { clockSkew: config.petstoreAuthCredentials.oauthClockSkew, } as OAuthConfiguration ), apiKey: config.apiKeyCredentials && customHeaderAuthenticationProvider(config.apiKeyCredentials), }; return compositeAuthenticationProvider< keyof typeof authConfig, typeof authConfig >(authConfig); } function petstoreAuthTokenProvider( petstoreAuth: () => PetstoreAuthManager | undefined, defaultProvider: | (( lastOAuthToken: OauthToken | undefined, authManager: PetstoreAuthManager ) => Promise) | undefined ): ((token: OauthToken | undefined) => Promise) | undefined { if (defaultProvider === undefined) { return undefined; } return (token: OauthToken | undefined) => { const manager = petstoreAuth(); if (manager === undefined) { throw Error('Unable to find the OAuthManager instance'); } return defaultProvider(token, manager); }; }