|
| 1 | +# Auth0 Node.js SDK - GitHub Copilot Instructions |
| 2 | + |
| 3 | +## Architecture Overview |
| 4 | + |
| 5 | +This is the **Auth0 Node.js SDK v5** - a TypeScript SDK providing Auth0 Authentication and Management API clients. The codebase uses **Fern-generated code** for the Management API with custom wrappers. |
| 6 | + |
| 7 | +### Key Components |
| 8 | + |
| 9 | +- **`src/management/`** - Fern-generated Management API client (auto-generated, don't edit directly) |
| 10 | +- **`src/management/wrapper/`** - Custom wrappers around Fern client for enhanced DX |
| 11 | +- **`src/auth/`** - Hand-written Authentication API client |
| 12 | +- **`src/userinfo/`** - UserInfo API client |
| 13 | +- **`legacy/`** - v4 compatibility layer for migration |
| 14 | + |
| 15 | +### Dual Module System |
| 16 | + |
| 17 | +- **CJS build**: `dist/cjs/` (main entry) |
| 18 | +- **ESM build**: `dist/esm/` (with `.mjs` extensions via `scripts/rename-to-esm-files.js`) |
| 19 | +- **Legacy exports**: `auth0/legacy` path for v4 compatibility |
| 20 | + |
| 21 | +## Development Patterns |
| 22 | + |
| 23 | +### Code Generation Awareness |
| 24 | + |
| 25 | +- Files with `* This file was auto-generated by Fern from our API Definition.` are **READ-ONLY** |
| 26 | +- Use wrapper classes in `src/management/wrapper/` for customizations |
| 27 | +- Authentication API (`src/auth/`) is hand-written - safe to edit |
| 28 | + |
| 29 | +### Testing Strategy |
| 30 | + |
| 31 | +Jest config has **4 separate projects**: |
| 32 | + |
| 33 | +```bash |
| 34 | +yarn test:unit # Unit tests (src/management/tests) |
| 35 | +yarn test:browser # Browser environment tests |
| 36 | +yarn test:wire # Integration tests with mock server |
| 37 | +yarn test # Root-level tests (tests/ directory) |
| 38 | +``` |
| 39 | + |
| 40 | +### Build System |
| 41 | + |
| 42 | +```bash |
| 43 | +yarn build # Both CJS and ESM builds |
| 44 | +yarn build:cjs # CommonJS build |
| 45 | +yarn build:esm # ESM build + rename to .mjs files |
| 46 | +``` |
| 47 | + |
| 48 | +### TypeScript Configuration |
| 49 | + |
| 50 | +- **`tsconfig.base.json`** - Base config targeting Management API |
| 51 | +- **`tsconfig.cjs.json`** - CommonJS build |
| 52 | +- **`tsconfig.esm.json`** - ESM build |
| 53 | +- Uses `baseUrl: "src/management"` for Management API focus |
| 54 | + |
| 55 | +## API Client Patterns |
| 56 | + |
| 57 | +### Management Client Wrapper Pattern |
| 58 | + |
| 59 | +```typescript |
| 60 | +// Don't edit src/management/Client.ts (Fern-generated) |
| 61 | +// Use src/management/wrapper/ManagementClient.ts instead |
| 62 | +export class ManagementClient { |
| 63 | + private _client: FernClient; |
| 64 | + // Custom logic, token handling, telemetry |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### Request Options Pattern |
| 69 | + |
| 70 | +Use helper functions from `src/management/request-options.js`: |
| 71 | + |
| 72 | +```typescript |
| 73 | +import { withTimeout, withRetries, withHeaders, CustomDomainHeader } from "auth0"; |
| 74 | + |
| 75 | +const options = { |
| 76 | + ...withTimeout(30), |
| 77 | + ...withRetries(3), |
| 78 | + ...CustomDomainHeader("auth.example.com"), |
| 79 | +}; |
| 80 | +``` |
| 81 | + |
| 82 | +### Error Handling |
| 83 | + |
| 84 | +- **ManagementError** - For Management API errors |
| 85 | +- **AuthApiError** - For Authentication API errors |
| 86 | +- Use `.withRawResponse()` for accessing raw HTTP response data |
| 87 | + |
| 88 | +## File Organization Rules |
| 89 | + |
| 90 | +### DO NOT EDIT |
| 91 | + |
| 92 | +- `src/management/Client.ts` and `src/management/api/` (Fern-generated) |
| 93 | +- Anything with "auto-generated by Fern" comment |
| 94 | + |
| 95 | +### SAFE TO EDIT |
| 96 | + |
| 97 | +- `src/management/wrapper/` - Custom wrapper logic |
| 98 | +- `src/auth/` - Authentication API implementation |
| 99 | +- `src/userinfo/` - UserInfo API implementation |
| 100 | +- `tests/` and `src/management/tests/` - Test files |
| 101 | + |
| 102 | +### Legacy Support |
| 103 | + |
| 104 | +- `legacy/exports/` contains v4 API compatibility |
| 105 | +- Use `import { ... } from 'auth0/legacy'` for v4 migration |
| 106 | +- See `v5_MIGRATION_GUIDE.md` for breaking changes |
| 107 | + |
| 108 | +## Common Tasks |
| 109 | + |
| 110 | +### Adding Management API Features |
| 111 | + |
| 112 | +1. Extend `src/management/wrapper/ManagementClient.ts` |
| 113 | +2. Add types to `src/management/wrapper/` if needed |
| 114 | +3. Add tests to `src/management/tests/` |
| 115 | + |
| 116 | +### Testing Integration Points |
| 117 | + |
| 118 | +```bash |
| 119 | +yarn test:wire # Tests with mock server setup |
| 120 | +``` |
| 121 | + |
| 122 | +Mock server setup in `src/management/tests/mock-server/setup.ts` |
| 123 | + |
| 124 | +### Module Resolution |
| 125 | + |
| 126 | +- Uses `.js` extensions in imports (TS will resolve to `.ts`) |
| 127 | +- ESM build renames `.js` → `.mjs` and `.d.ts` → `.d.mts` |
| 128 | +- Browser field in `package.json` excludes Node.js modules |
| 129 | + |
| 130 | +Remember: This SDK prioritizes backward compatibility through the legacy export while modernizing the core API with Fern generation. |
0 commit comments