diff --git a/EXAMPLES.md b/EXAMPLES.md index 4d0b07685..c9c510ed0 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -11,6 +11,9 @@ - [Paginate through a list of logs using checkpoint pagination](#paginate-through-a-list-of-logs-using-checkpoint-pagination) - [Import users from a JSON file](#import-users-from-a-json-file) - [Update a user's user_metadata](#update-a-users-user_metadata) +- [Customizing the request](#customizing-the-request) + - [Passing custom options to fetch](#passing-custom-options-to-fetch) + - [Overriding `fetch`](#overriding-fetch) ## Authentication Client @@ -230,3 +233,44 @@ const management = new ManagementClient({ await management.users.update({ id: '{user id}' }, { user_metadata: { foo: 'bar' } }); ``` + +## Customizing the request + +### Passing custom options to fetch + +```js +import https from 'https'; +import { ManagementClient } from 'auth0'; + +const management = new ManagementClient({ + domain: '{YOUR_TENANT_AND REGION}.auth0.com', + clientId: '{YOUR_CLIENT_ID}', + clientSecret: '{YOUR_CLIENT_SECRET}', + headers: { 'foo': 'applied to all requests' }, + agent: new https.Agent({ ... }), + httpTimeout: 5000 +}); + +await management.users.get({ id: '{user id}' }, { headers: { 'bar': 'applied to this request' } }); +``` + +### Overriding `fetch` + +```js +import { ManagementClient } from 'auth0'; +import { myFetch } from './fetch'; + +const management = new ManagementClient({ + domain: '{YOUR_TENANT_AND REGION}.auth0.com', + clientId: '{YOUR_CLIENT_ID}', + clientSecret: '{YOUR_CLIENT_SECRET}', + async fetch(url, init) { + log('before', url, init.method); + const res = await myFetch(url, init); + log('after', url, init.method, res.status); + return res; + }, +}); + +await management.users.get({ id: '{user id}' }); +``` diff --git a/src/auth/base-auth-api.ts b/src/auth/base-auth-api.ts index a32d836ee..027245dbf 100644 --- a/src/auth/base-auth-api.ts +++ b/src/auth/base-auth-api.ts @@ -12,6 +12,7 @@ import { } from './client-authentication.js'; import { IDTokenValidator } from './id-token-validator.js'; import { GrantOptions, TokenSet } from './oauth.js'; +import { TelemetryMiddleware } from '../lib/middleware/telemetry-middleware.js'; export interface AuthenticationClientOptions extends ClientOptions { domain: string; @@ -96,6 +97,7 @@ export class BaseAuthAPI extends BaseAPI { super({ ...options, baseUrl: `https://${options.domain}`, + middleware: options.telemetry !== false ? [new TelemetryMiddleware(options)] : [], parseError, retry: { enabled: false, ...options.retry }, }); diff --git a/src/auth/index.ts b/src/auth/index.ts index b308ee705..a5f8faed4 100644 --- a/src/auth/index.ts +++ b/src/auth/index.ts @@ -1,4 +1,3 @@ -import { TelemetryMiddleware } from '../lib/middleware/telemetry-middleware.js'; import { AuthenticationClientOptions } from './base-auth-api.js'; import { Database } from './database.js'; import { OAuth } from './oauth.js'; @@ -16,10 +15,6 @@ export class AuthenticationClient { passwordless: Passwordless; constructor(options: AuthenticationClientOptions) { - if (options.telemetry !== false) { - options.middleware = [...(options.middleware || []), new TelemetryMiddleware(options)]; - } - this.database = new Database(options); this.oauth = new OAuth(options); this.passwordless = new Passwordless(options); diff --git a/src/lib/models.ts b/src/lib/models.ts index ef306bf09..2ffbb142c 100644 --- a/src/lib/models.ts +++ b/src/lib/models.ts @@ -5,7 +5,8 @@ import { RetryConfiguration } from './retry.js'; */ export type FetchAPI = (url: URL | RequestInfo, init?: RequestInit) => Promise; -export interface ClientOptions extends Omit { +export interface ClientOptions + extends Omit { telemetry?: boolean; clientInfo?: { name: string; [key: string]: unknown }; } @@ -16,7 +17,7 @@ export interface Configuration { /** * Provide your own fetch implementation. */ - fetchApi?: FetchAPI; + fetch?: FetchAPI; /** * Provide a middleware that will run either before the request, after the request or when the request fails. */ diff --git a/src/lib/runtime.ts b/src/lib/runtime.ts index 46573b862..bfcf5ccbe 100644 --- a/src/lib/runtime.ts +++ b/src/lib/runtime.ts @@ -31,7 +31,7 @@ export class BaseAPI { } this.middleware = configuration.middleware || []; - this.fetchApi = configuration.fetchApi || fetch; + this.fetchApi = configuration.fetch || fetch; this.parseError = configuration.parseError; this.timeoutDuration = typeof configuration.timeoutDuration === 'number' ? configuration.timeoutDuration : 10000; diff --git a/src/management/management-client.ts b/src/management/management-client.ts index 39ec8ed22..08c04e6f1 100644 --- a/src/management/management-client.ts +++ b/src/management/management-client.ts @@ -70,7 +70,6 @@ export class ManagementClient extends ManagementClientBase { ...options, baseUrl: `https://${options.domain}/api/v2`, middleware: [ - ...(options.middleware || []), new TokenProviderMiddleware(options), ...(options.telemetry !== false ? [new TelemetryMiddleware(options)] : []), ], diff --git a/src/userinfo/index.ts b/src/userinfo/index.ts index 24529e95e..6bd6c2dc7 100644 --- a/src/userinfo/index.ts +++ b/src/userinfo/index.ts @@ -81,10 +81,7 @@ export class UserInfoClient extends BaseAPI { super({ ...options, baseUrl: `https://${options.domain}`, - middleware: [ - ...(options.middleware || []), - ...(options.telemetry !== false ? [new TelemetryMiddleware(options)] : []), - ], + middleware: options.telemetry !== false ? [new TelemetryMiddleware(options)] : [], parseError, }); }