From 73b12eae5bc68d174cfeac6511349585cb39c1f0 Mon Sep 17 00:00:00 2001 From: Adam Mcgrath Date: Wed, 30 Aug 2023 10:29:31 +0100 Subject: [PATCH 1/3] Rename fetch option, add request examples --- EXAMPLES.md | 100 ++++++++++++++++++++++++++++++++++++++++++++- src/lib/models.ts | 2 +- src/lib/runtime.ts | 2 +- 3 files changed, 101 insertions(+), 3 deletions(-) diff --git a/EXAMPLES.md b/EXAMPLES.md index 4d0b07685..fcdc60b4f 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -10,7 +10,11 @@ - [Paginate through a list of users](#paginate-through-a-list-of-users) - [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) + - [Update a user's user_metadata](#update-a-user-s-user-metadata) +- [Customizing the request](#customizing-the-request) + - [Passing custom options to fetch](#passing-custom-options-to-fetch) + - [Using middleware](#using-middleware) + - [Overriding `fetch`](#overriding-fetch) ## Authentication Client @@ -230,3 +234,97 @@ 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' } }); +``` + +### Using middleware + +```js +import { ManagementClient } from 'auth0'; + +const management = new ManagementClient({ + domain: '{YOUR_TENANT_AND REGION}.auth0.com', + clientId: '{YOUR_CLIENT_ID}', + clientSecret: '{YOUR_CLIENT_SECRET}', + middleware: [ + { + async pre({ url, init }) { + // Run some code before every request + log(url, init.method); + // optionally modify the url of fetchoptions + return { url, init: { ...init, priority: 'high' } }; + }, + }, + { + // Runs after all retries, before reading the response + async post({ response }) { + // Run some code after every request + log(url, init.method, response); + // Optionall modify the response + const json = await response.json(); + return Response.json({ ...json, foo: 'bar' }); + }, + }, + { + // Runs after all retries, before reading the response + async onError({ fetch, url, init, error, response }) { + // Run some code when the request fails. + log(error); + // Optionally return a backup response + return Response.json({ foo: 'bar' }); + }, + }, + { + async onError({ fetch, url, init, error, response }) { + if (response.status === 429) { + // Retry the response using your own retry logic (rather than the SDK's exponential backoff) + return fetch(url, init); + } + // throw a custom error + throw new Error('foo'); + }, + }, + ], +}); + +await management.users.get({ id: '{user id}' }); +``` + +### 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/lib/models.ts b/src/lib/models.ts index ef306bf09..bc96c249b 100644 --- a/src/lib/models.ts +++ b/src/lib/models.ts @@ -16,7 +16,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; From 657bfb5b4d23c06591547446971ef142c0bc11b0 Mon Sep 17 00:00:00 2001 From: Adam Mcgrath Date: Wed, 30 Aug 2023 10:34:02 +0100 Subject: [PATCH 2/3] fix link --- EXAMPLES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EXAMPLES.md b/EXAMPLES.md index fcdc60b4f..9465abe96 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -10,7 +10,7 @@ - [Paginate through a list of users](#paginate-through-a-list-of-users) - [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-user-s-user-metadata) + - [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) - [Using middleware](#using-middleware) From 460c5a35b50184ad7a8fdf63cc0bb4cd2acb26bd Mon Sep 17 00:00:00 2001 From: Adam Mcgrath Date: Wed, 30 Aug 2023 10:50:46 +0100 Subject: [PATCH 3/3] Remove middleware option --- EXAMPLES.md | 54 ----------------------------- src/auth/base-auth-api.ts | 2 ++ src/auth/index.ts | 5 --- src/lib/models.ts | 3 +- src/management/management-client.ts | 1 - src/userinfo/index.ts | 5 +-- 6 files changed, 5 insertions(+), 65 deletions(-) diff --git a/EXAMPLES.md b/EXAMPLES.md index 9465abe96..c9c510ed0 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -13,7 +13,6 @@ - [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) - - [Using middleware](#using-middleware) - [Overriding `fetch`](#overriding-fetch) ## Authentication Client @@ -255,59 +254,6 @@ const management = new ManagementClient({ await management.users.get({ id: '{user id}' }, { headers: { 'bar': 'applied to this request' } }); ``` -### Using middleware - -```js -import { ManagementClient } from 'auth0'; - -const management = new ManagementClient({ - domain: '{YOUR_TENANT_AND REGION}.auth0.com', - clientId: '{YOUR_CLIENT_ID}', - clientSecret: '{YOUR_CLIENT_SECRET}', - middleware: [ - { - async pre({ url, init }) { - // Run some code before every request - log(url, init.method); - // optionally modify the url of fetchoptions - return { url, init: { ...init, priority: 'high' } }; - }, - }, - { - // Runs after all retries, before reading the response - async post({ response }) { - // Run some code after every request - log(url, init.method, response); - // Optionall modify the response - const json = await response.json(); - return Response.json({ ...json, foo: 'bar' }); - }, - }, - { - // Runs after all retries, before reading the response - async onError({ fetch, url, init, error, response }) { - // Run some code when the request fails. - log(error); - // Optionally return a backup response - return Response.json({ foo: 'bar' }); - }, - }, - { - async onError({ fetch, url, init, error, response }) { - if (response.status === 429) { - // Retry the response using your own retry logic (rather than the SDK's exponential backoff) - return fetch(url, init); - } - // throw a custom error - throw new Error('foo'); - }, - }, - ], -}); - -await management.users.get({ id: '{user id}' }); -``` - ### Overriding `fetch` ```js 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 bc96c249b..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 }; } 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, }); }