Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename fetch option, add request examples #923

Merged
merged 5 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}' });
```
2 changes: 2 additions & 0 deletions src/auth/base-auth-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 },
});
Expand Down
5 changes: 0 additions & 5 deletions src/auth/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { RetryConfiguration } from './retry.js';
*/
export type FetchAPI = (url: URL | RequestInfo, init?: RequestInit) => Promise<Response>;

export interface ClientOptions extends Omit<Configuration, 'baseUrl' | 'parseError'> {
export interface ClientOptions
extends Omit<Configuration, 'baseUrl' | 'parseError' | 'middleware'> {
telemetry?: boolean;
clientInfo?: { name: string; [key: string]: unknown };
}
Expand All @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/lib/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/management/management-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)] : []),
],
Expand Down
5 changes: 1 addition & 4 deletions src/userinfo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
Expand Down