Skip to content

Commit 490e563

Browse files
feat(management): add sub-package exports and management auth helper (#1373)
Co-authored-by: Ankita Tripathi <51994119+ankita10119@users.noreply.github.com>
1 parent 8cb0b20 commit 490e563

150 files changed

Lines changed: 2579 additions & 108 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,99 @@ const management = new ManagementClient({
7676
});
7777
```
7878

79+
#### Individual Management sub-clients (smaller bundles)
80+
81+
If you only need a few Management API resources, you can import them individually instead of
82+
the full `ManagementClient`. Each resource has its own entry point (for example
83+
`auth0/clients`, `auth0/users`, `auth0/connections`), so a bundler ships only the resources
84+
you use. This keeps bundles small on size-constrained runtimes such as Cloudflare Workers.
85+
86+
To avoid wiring up authentication for every client, use `createManagementAuth` from
87+
`auth0/management`. It handles the token once (fetching and refreshing via client
88+
credentials, or accepting a static token) and gives you an options object you spread into
89+
any sub-client. The token is cached and shared, so you are not re-authenticating per client.
90+
91+
```js
92+
import { createManagementAuth } from "auth0/management";
93+
import { ClientsClient } from "auth0/clients";
94+
import { UsersClient } from "auth0/users";
95+
96+
// Configure auth once and reuse it across clients.
97+
const auth = createManagementAuth({
98+
domain: "{YOUR_TENANT_AND_REGION}.auth0.com",
99+
clientId: "{YOUR_CLIENT_ID}",
100+
clientSecret: "{YOUR_CLIENT_SECRET}",
101+
});
102+
103+
// Create each sub-client once and reuse the instances throughout your app.
104+
export const clients = new ClientsClient(auth.clientOptions);
105+
export const users = new UsersClient(auth.clientOptions);
106+
107+
await users.list({ page: 0, per_page: 10 });
108+
```
109+
110+
You can also pass a static token:
111+
112+
```js
113+
const auth = createManagementAuth({
114+
domain: "{YOUR_TENANT_AND_REGION}.auth0.com",
115+
token: "{YOUR_API_V2_TOKEN}",
116+
});
117+
```
118+
119+
For lower-level control over the token lifecycle, `TokenProvider` is also exported from
120+
`auth0/management`. It performs the client credentials grant and caches the token until
121+
shortly before it expires:
122+
123+
```js
124+
import { TokenProvider } from "auth0/management";
125+
import { ClientsClient } from "auth0/clients";
126+
127+
const tokenProvider = new TokenProvider({
128+
domain: "{YOUR_TENANT_AND_REGION}.auth0.com",
129+
clientId: "{YOUR_CLIENT_ID}",
130+
clientSecret: "{YOUR_CLIENT_SECRET}",
131+
audience: "https://{YOUR_TENANT_AND_REGION}.auth0.com/api/v2/",
132+
});
133+
134+
const clients = new ClientsClient({
135+
baseUrl: "https://{YOUR_TENANT_AND_REGION}.auth0.com/api/v2",
136+
token: () => tokenProvider.getAccessToken(),
137+
});
138+
```
139+
140+
Request and response types for these clients live under the shared `Management` namespace and
141+
are imported separately with `import type { Management } from "auth0"`:
142+
143+
```ts
144+
import type { Management } from "auth0";
145+
146+
const body: Management.CreateClientRequestContent = { name: "My App" };
147+
const created: Management.CreateClientResponseContent = await clients.create(body);
148+
```
149+
150+
Because they are TypeScript interfaces, `import type` is erased at compile time, so importing
151+
types from the root `auth0` entry adds nothing to your bundle and does not pull in the full
152+
`ManagementClient`.
153+
154+
**Recommendations for small bundles**
155+
156+
- Import each client as a **value** from its own entry point (`auth0/clients`, `auth0/users`,
157+
and so on), not from the root `auth0`. A value import from the root pulls the full
158+
`ManagementClient` and all resources into the module graph.
159+
- Import request and response types with `import type { Management } from "auth0"`. Types are
160+
erased, so this is always free regardless of the entry point.
161+
- Prefer `import type` over a plain `import` for anything you only use in type positions. It
162+
guarantees the import is erased and never accidentally ships runtime code (the one thing
163+
that does add bytes is referencing an enum **value**, such as `OauthScope.CreateActions`).
164+
- Configure authentication once with `createManagementAuth` (or a single shared
165+
`TokenProvider`) and reuse the returned `clientOptions` across every sub-client. Create each
166+
sub-client once and reuse the instance rather than constructing new clients per request.
167+
168+
> These smaller bundles rely on tree-shaking, so they apply when you consume the SDK as ESM
169+
> through a bundler. A plain CommonJS `require()` cannot tree-shake and loads the full
170+
> resource graph.
171+
79172
#### UserInfo API Client
80173

81174
This client can be used to retrieve user profile information.

0 commit comments

Comments
 (0)