Skip to content

Commit bb886ad

Browse files
committed
feat(sdk): rename sdk classes, modules, and debug namespaces for consistency
1 parent 342f797 commit bb886ad

28 files changed

+232
-193
lines changed

README.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ pnpm add @strapi/sdk-js
8383
To interact with your Strapi backend, initialize the SDK with your Strapi API base URL:
8484

8585
```typescript
86-
import { strapiSDK } from '@strapi/sdk-js';
86+
import { strapi } from '@strapi/sdk-js';
8787

88-
const sdk = strapiSDK({ baseURL: 'http://localhost:1337/api' });
88+
const sdk = strapi({ baseURL: 'http://localhost:1337/api' });
8989
```
9090

9191
Alternatively, use a `<script>` tag in a browser environment:
@@ -94,7 +94,7 @@ Alternatively, use a `<script>` tag in a browser environment:
9494
<script src="https://cdn.jsdelivr.net/npm/@strapi/sdk-js"></script>
9595

9696
<script>
97-
const sdk = strapi.strapiSDK({ baseURL: 'http://localhost:1337/api' });
97+
const sdk = strapi.strapi({ baseURL: 'http://localhost:1337/api' });
9898
</script>
9999
```
100100

@@ -107,7 +107,7 @@ The SDK supports multiple authentication strategies for accessing authenticated
107107
If your Strapi instance uses API tokens, configure the SDK like this:
108108

109109
```typescript
110-
const sdk = strapiSDK({
110+
const sdk = strapi({
111111
baseURL: 'http://localhost:1337/api',
112112
auth: {
113113
strategy: 'api-token',
@@ -230,25 +230,25 @@ For **browser environments**, debug capabilities are intentionally turned off to
230230
The `debug` tool allows you to control logs using wildcard patterns (`*`):
231231

232232
- `*`: enable all logs.
233-
- `sdk:module`: enable logs for a specific module.
234-
- `sdk:module1,sdk:module2`: enable logs for multiple modules.
235-
- `sdk:*`: match all namespaces under `sdk`.
236-
- `sdk:*,-sdk:module2`: enable all logs except those from `sdk:module2`.
233+
- `strapi:module`: enable logs for a specific module.
234+
- `strapi:module1,sdk:module2`: enable logs for multiple modules.
235+
- `strapi:*`: match all namespaces under `strapi`.
236+
- `strapi:*,-strapi:module2`: enable all logs except those from `strapi:module2`.
237237

238238
### Namespaces
239239

240240
Below is a list of available namespaces to use:
241241

242-
| Namespace | Description |
243-
|---------------------------------------|-------------------------------------------------------------------------------------------|
244-
| `sdk:core` | Logs SDK initialization, configuration validation, and HTTP client setup. |
245-
| `sdk:validators:sdk` | Logs details related to SDK configuration validation. |
246-
| `sdk:validators:url` | Logs URL validation processes. |
247-
| `sdk:http` | Logs HTTP client setup, request processing, and response/error handling. |
248-
| `sdk:auth:factory` | Logs the registration and creation of authentication providers. |
249-
| `sdk:auth:manager` | Logs authentication lifecycle management. |
250-
| `sdk:auth:provider:api-token` | Logs operations related to API token authentication. |
251-
| `sdk:auth:provider:users-permissions` | Logs operations related to user and permissions-based authentication. |
252-
| `sdk:ct:collection` | Logs interactions with collection-type content managers. |
253-
| `sdk:ct:single` | Logs interactions with single-type content managers. |
254-
| `sdk:utils:url-helper` | Logs URL helper utility operations (e.g., appending query parameters or formatting URLs). |
242+
| Namespace | Description |
243+
|------------------------------------------|-------------------------------------------------------------------------------------------|
244+
| `strapi:core` | Logs SDK initialization, configuration validation, and HTTP client setup. |
245+
| `strapi:validators:config` | Logs details related to SDK configuration validation. |
246+
| `strapi:validators:url` | Logs URL validation processes. |
247+
| `strapi:http` | Logs HTTP client setup, request processing, and response/error handling. |
248+
| `strapi:auth:factory` | Logs the registration and creation of authentication providers. |
249+
| `strapi:auth:manager` | Logs authentication lifecycle management. |
250+
| `strapi:auth:provider:api-token` | Logs operations related to API token authentication. |
251+
| `strapi:auth:provider:users-permissions` | Logs operations related to user and permissions-based authentication. |
252+
| `strapi:ct:collection` | Logs interactions with collection-type content managers. |
253+
| `strapi:ct:single` | Logs interactions with single-type content managers. |
254+
| `strapi:utils:url-helper` | Logs URL helper utility operations (e.g., appending query parameters or formatting URLs). |

index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const { strapi } = require('./dist/bundle.cjs');
2+
3+
const sdk = strapi({
4+
baseURL: 'http://localhost:1337/api',
5+
auth: {
6+
strategy: 'api-token',
7+
options: {
8+
// identifier: 'bob@bob.com',
9+
// password: 'Admin1Admin',
10+
token:
11+
'f45e93601ace4f1806b69ca2fa1567bd7f5bff5d608af870fa89664b149b05493fb030973e5628aa288878f6be01df430a4505fe32b5bd29632ebedba69b46b54e49d638aecd78cfb07ec02a9ad5867ecea215711949d43ed3ae3d8d8739aeab83f4b0e4ce23405a4328aba8ea5c1c06cbc7b2bdbdfb597f8033841e47671f79',
12+
},
13+
},
14+
});
15+
16+
// global interceptors
17+
// per request interceptors
18+
19+
const cats = sdk.collection('categories');
20+
21+
cats
22+
.find({
23+
fields: ['name', 'locale'],
24+
filters: { locale: 'en' },
25+
sort: 'updatedAt:desc',
26+
})
27+
.then(({ data, meta }) => {
28+
console.log('Categories:', data.slice(0, 2));
29+
console.log('Pagination', meta.pagination);
30+
31+
return data.at(0);
32+
})
33+
.then(async (cat) => {
34+
const x = await cats.findOne(cat.documentId);
35+
36+
console.log('Found one');
37+
console.log(x);
38+
})
39+
.catch((error) => {
40+
console.error('Error fetching categories:', error.name, error.message);
41+
});

src/auth/factory/factory.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import createDebug from 'debug';
22

3-
import { StrapiSDKError } from '../../errors';
3+
import { StrapiError } from '../../errors';
44

55
import type { AuthProviderCreator, AuthProviderMap, CreateAuthProviderParams } from './types';
66
import type { AuthProvider } from '../providers';
77

8-
const debug = createDebug('sdk:auth:factory');
8+
const debug = createDebug('strapi:auth:factory');
99

1010
/**
1111
* A factory class responsible for creating and managing authentication providers.
@@ -26,7 +26,7 @@ export class AuthProviderFactory<T_Providers extends AuthProviderMap = {}> {
2626
*
2727
* @returns An instance of an AuthProvider initialized with the given options.
2828
*
29-
* @throws {StrapiSDKError} Throws an error if the specified strategy is not registered in the factory.
29+
* @throws {StrapiError} Throws an error if the specified strategy is not registered in the factory.
3030
*
3131
* @example
3232
* ```typescript
@@ -48,7 +48,7 @@ export class AuthProviderFactory<T_Providers extends AuthProviderMap = {}> {
4848

4949
if (!creator) {
5050
debug('the %o auth strategy is not registered, skipping', authStrategy);
51-
throw new StrapiSDKError(`Auth strategy "${authStrategy}" is not supported.`);
51+
throw new StrapiError(`Auth strategy "${authStrategy}" is not supported.`);
5252
}
5353

5454
const instance = creator(options);

src/auth/manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
UsersPermissionsAuthProviderOptions,
1212
} from './providers';
1313

14-
const debug = createDebug('sdk:auth:manager');
14+
const debug = createDebug('strapi:auth:manager');
1515

1616
/**
1717
* Manages authentication by using different authentication providers and strategies.

src/auth/providers/abstract.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export abstract class AbstractAuthProvider<T = unknown> implements AuthProvider
5151
* It is called within the constructor to ensure that all required options adhere
5252
* to the expected format or values before proceeding with operational methods.
5353
*
54-
* @throws {StrapiSDKValidationError} If the validation fails due to invalid or missing options.
54+
* @throws {StrapiValidationError} If the validation fails due to invalid or missing options.
5555
*/
5656
protected abstract preflightValidation(): void;
5757

src/auth/providers/api-token.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import createDebug from 'debug';
22

3-
import { StrapiSDKValidationError } from '../../errors';
3+
import { StrapiValidationError } from '../../errors';
44

55
import { AbstractAuthProvider } from './abstract';
66

7-
const debug = createDebug('sdk:auth:provider:api-token');
7+
const debug = createDebug('strapi:auth:provider:api-token');
88

99
const API_TOKEN_AUTH_STRATEGY_IDENTIFIER = 'api-token';
1010

@@ -41,7 +41,7 @@ export class ApiTokenAuthProvider extends AbstractAuthProvider<ApiTokenAuthProvi
4141
if ((typeof this._token as unknown) !== 'string' || this._token.trim().length === 0) {
4242
debug('invalid api token provided: %o (%o)', this._token, typeof this._token);
4343

44-
throw new StrapiSDKValidationError(
44+
throw new StrapiValidationError(
4545
`A valid API token is required when using the api-token auth strategy. Got "${this._token}"`
4646
);
4747
}

src/auth/providers/users-permissions.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import createDebug from 'debug';
22

3-
import { StrapiSDKValidationError } from '../../errors';
3+
import { StrapiValidationError } from '../../errors';
44
import { HttpClient } from '../../http';
55

66
import { AbstractAuthProvider } from './abstract';
77

8-
const debug = createDebug('sdk:auth:provider:users-permissions');
8+
const debug = createDebug('strapi:auth:provider:users-permissions');
99

1010
const USERS_PERMISSIONS_AUTH_STRATEGY_IDENTIFIER = 'users-permissions';
1111
const LOCAL_AUTH_ENDPOINT = '/auth/local';
@@ -68,7 +68,7 @@ export class UsersPermissionsAuthProvider extends AbstractAuthProvider<UsersPerm
6868
) {
6969
debug('invalid options provided: %s (%s)', this._options, typeof this._options);
7070

71-
throw new StrapiSDKValidationError(
71+
throw new StrapiValidationError(
7272
'Missing valid options for initializing the Users & Permissions auth provider.'
7373
);
7474
}
@@ -78,15 +78,15 @@ export class UsersPermissionsAuthProvider extends AbstractAuthProvider<UsersPerm
7878
if ((typeof identifier as unknown) !== 'string') {
7979
debug('invalid identifier provided: %s (%s)', identifier, typeof identifier);
8080

81-
throw new StrapiSDKValidationError(
81+
throw new StrapiValidationError(
8282
`The "identifier" option must be a string, but got "${typeof identifier}"`
8383
);
8484
}
8585

8686
if ((typeof password as unknown) !== 'string') {
8787
debug('invalid password provided: %s (%s)', password, typeof password);
8888

89-
throw new StrapiSDKValidationError(
89+
throw new StrapiValidationError(
9090
`The "password" option must be a string, but got "${typeof password}"`
9191
);
9292
}

src/content-types/collection/manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from '../../types/content-api';
99
import { URLHelper } from '../../utilities';
1010

11-
const debug = createDebug('sdk:ct:collection');
11+
const debug = createDebug('strapi:ct:collection');
1212

1313
/**
1414
* A service class designed for interacting with a collection-type resource in a Strapi app.

src/content-types/single/manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { HttpClient } from '../../http';
44
import { BaseQueryParams, GenericDocumentResponse } from '../../types/content-api';
55
import { URLHelper } from '../../utilities';
66

7-
const debug = createDebug('sdk:ct:single');
7+
const debug = createDebug('strapi:ct:single');
88

99
/**
1010
* A service class designed for interacting with a single-type resource in a Strapi app.

src/errors/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from './sdk';
1+
export * from './strapi';
22
export * from './url';
33
export * from './http';

src/errors/sdk.ts renamed to src/errors/strapi.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export class StrapiSDKError extends Error {
1+
export class StrapiError extends Error {
22
constructor(
33
cause: unknown = undefined,
44
message: string = 'An error occurred in the Strapi SDK. Please check the logs for more information.'
@@ -9,7 +9,7 @@ export class StrapiSDKError extends Error {
99
}
1010
}
1111

12-
export class StrapiSDKValidationError extends StrapiSDKError {
12+
export class StrapiValidationError extends StrapiError {
1313
constructor(
1414
cause: unknown = undefined,
1515
message: string = 'Some of the provided values are not valid.'
@@ -18,7 +18,7 @@ export class StrapiSDKValidationError extends StrapiSDKError {
1818
}
1919
}
2020

21-
export class StrapiSDKInitializationError extends StrapiSDKError {
21+
export class StrapiInitializationError extends StrapiError {
2222
constructor(cause: unknown = undefined, message: string = 'Could not initialize the Strapi SDK') {
2323
super(cause, message);
2424
}

src/http/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { HttpInterceptorManager } from './interceptor-manager';
1818

1919
import type { HttpClientConfig, InterceptorManagerMap } from './types';
2020

21-
const debug = createDebug('sdk:http');
21+
const debug = createDebug('strapi:http');
2222

2323
/**
2424
* Strapi SDK's HTTP Client

src/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { StrapiSDK } from './sdk';
2-
import { StrapiSDKValidator } from './validators';
1+
import { Strapi } from './sdk';
2+
import { StrapiConfigValidator } from './validators';
33

4-
import type { StrapiSDKConfig } from './sdk';
4+
import type { StrapiConfig } from './sdk';
55

66
export * from './errors';
77

@@ -23,7 +23,7 @@ export * from './errors';
2323
* @example
2424
* ```typescript
2525
* // Basic configuration using API token auth
26-
* const sdkConfig = {
26+
* const config = {
2727
* baseURL: 'https://api.example.com',
2828
* auth: {
2929
* strategy: 'api-token',
@@ -32,25 +32,25 @@ export * from './errors';
3232
* };
3333
*
3434
* // Create the SDK instance
35-
* const strapiSDK = strapiSDK(sdkConfig);
35+
* const sdk = strapi(config);
3636
*
3737
* // Using the SDK to fetch content from a custom endpoint
38-
* const response = await strapiSDK.fetch('/content-endpoint');
38+
* const response = await sdk.fetch('/content-endpoint');
3939
* const data = await response.json();
4040
*
4141
* console.log(data);
4242
* ```
4343
*
44-
* @throws {StrapiSDKInitializationError} If the provided baseURL does not conform to a valid HTTP or HTTPS URL,
44+
* @throws {StrapiInitializationError} If the provided baseURL doesn't conform to a valid HTTP or HTTPS URL,
4545
* or if the auth configuration is invalid.
4646
*/
47-
export const strapiSDK = (config: StrapiSDKConfig) => {
48-
const sdkValidator = new StrapiSDKValidator();
47+
export const strapi = (config: StrapiConfig) => {
48+
const configValidator = new StrapiConfigValidator();
4949

50-
return new StrapiSDK<typeof config>(
50+
return new Strapi<typeof config>(
5151
// Properties
5252
config,
5353
// Dependencies
54-
sdkValidator
54+
configValidator
5555
);
5656
};

0 commit comments

Comments
 (0)