Skip to content

Commit

Permalink
enhancement: rename the SDK constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Convly authored Jan 22, 2025
2 parents 4037a20 + 115b413 commit 9ac67c7
Show file tree
Hide file tree
Showing 27 changed files with 192 additions and 194 deletions.
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ pnpm add @strapi/sdk-js
To interact with your Strapi backend, initialize the SDK with your Strapi API base URL:

```typescript
import { strapiSDK } from '@strapi/sdk-js';
import { strapi } from '@strapi/sdk-js';

const sdk = strapiSDK({ baseURL: 'http://localhost:1337/api' });
const sdk = strapi({ baseURL: 'http://localhost:1337/api' });
```

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

<script>
const sdk = strapi.strapiSDK({ baseURL: 'http://localhost:1337/api' });
const sdk = strapi.strapi({ baseURL: 'http://localhost:1337/api' });
</script>
```

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

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

- `*`: enable all logs.
- `sdk:module`: enable logs for a specific module.
- `sdk:module1,sdk:module2`: enable logs for multiple modules.
- `sdk:*`: match all namespaces under `sdk`.
- `sdk:*,-sdk:module2`: enable all logs except those from `sdk:module2`.
- `strapi:module`: enable logs for a specific module.
- `strapi:module1,sdk:module2`: enable logs for multiple modules.
- `strapi:*`: match all namespaces under `strapi`.
- `strapi:*,-strapi:module2`: enable all logs except those from `strapi:module2`.

### Namespaces

Below is a list of available namespaces to use:

| Namespace | Description |
| ------------------------------------- | ----------------------------------------------------------------------------------------- |
| `sdk:core` | Logs SDK initialization, configuration validation, and HTTP client setup. |
| `sdk:validators:sdk` | Logs details related to SDK configuration validation. |
| `sdk:validators:url` | Logs URL validation processes. |
| `sdk:http` | Logs HTTP client setup, request processing, and response/error handling. |
| `sdk:auth:factory` | Logs the registration and creation of authentication providers. |
| `sdk:auth:manager` | Logs authentication lifecycle management. |
| `sdk:auth:provider:api-token` | Logs operations related to API token authentication. |
| `sdk:auth:provider:users-permissions` | Logs operations related to user and permissions-based authentication. |
| `sdk:ct:collection` | Logs interactions with collection-type content managers. |
| `sdk:ct:single` | Logs interactions with single-type content managers. |
| `sdk:utils:url-helper` | Logs URL helper utility operations (e.g., appending query parameters or formatting URLs). |
| Namespace | Description |
| ---------------------------------------- | ----------------------------------------------------------------------------------------- |
| `strapi:core` | Logs SDK initialization, configuration validation, and HTTP client setup. |
| `strapi:validators:config` | Logs details related to SDK configuration validation. |
| `strapi:validators:url` | Logs URL validation processes. |
| `strapi:http` | Logs HTTP client setup, request processing, and response/error handling. |
| `strapi:auth:factory` | Logs the registration and creation of authentication providers. |
| `strapi:auth:manager` | Logs authentication lifecycle management. |
| `strapi:auth:provider:api-token` | Logs operations related to API token authentication. |
| `strapi:auth:provider:users-permissions` | Logs operations related to user and permissions-based authentication. |
| `strapi:ct:collection` | Logs interactions with collection-type content managers. |
| `strapi:ct:single` | Logs interactions with single-type content managers. |
| `strapi:utils:url-helper` | Logs URL helper utility operations (e.g., appending query parameters or formatting URLs). |
8 changes: 4 additions & 4 deletions src/auth/factory/factory.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import createDebug from 'debug';

import { StrapiSDKError } from '../../errors';
import { StrapiError } from '../../errors';

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

const debug = createDebug('sdk:auth:factory');
const debug = createDebug('strapi:auth:factory');

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

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

const instance = creator(options);
Expand Down
2 changes: 1 addition & 1 deletion src/auth/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
UsersPermissionsAuthProviderOptions,
} from './providers';

const debug = createDebug('sdk:auth:manager');
const debug = createDebug('strapi:auth:manager');

/**
* Manages authentication by using different authentication providers and strategies.
Expand Down
2 changes: 1 addition & 1 deletion src/auth/providers/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export abstract class AbstractAuthProvider<T = unknown> implements AuthProvider
* It is called within the constructor to ensure that all required options adhere
* to the expected format or values before proceeding with operational methods.
*
* @throws {StrapiSDKValidationError} If the validation fails due to invalid or missing options.
* @throws {StrapiValidationError} If the validation fails due to invalid or missing options.
*/
protected abstract preflightValidation(): void;

Expand Down
6 changes: 3 additions & 3 deletions src/auth/providers/api-token.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import createDebug from 'debug';

import { StrapiSDKValidationError } from '../../errors';
import { StrapiValidationError } from '../../errors';

import { AbstractAuthProvider } from './abstract';

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

const API_TOKEN_AUTH_STRATEGY_IDENTIFIER = 'api-token';

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

throw new StrapiSDKValidationError(
throw new StrapiValidationError(
`A valid API token is required when using the api-token auth strategy. Got "${this._token}"`
);
}
Expand Down
10 changes: 5 additions & 5 deletions src/auth/providers/users-permissions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import createDebug from 'debug';

import { StrapiSDKValidationError } from '../../errors';
import { StrapiValidationError } from '../../errors';
import { HttpClient } from '../../http';

import { AbstractAuthProvider } from './abstract';

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

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

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

throw new StrapiSDKValidationError(
throw new StrapiValidationError(
`The "identifier" option must be a string, but got "${typeof identifier}"`
);
}

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

throw new StrapiSDKValidationError(
throw new StrapiValidationError(
`The "password" option must be a string, but got "${typeof password}"`
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/content-types/collection/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../../types/content-api';
import { URLHelper } from '../../utilities';

const debug = createDebug('sdk:ct:collection');
const debug = createDebug('strapi:ct:collection');

/**
* A service class designed for interacting with a collection-type resource in a Strapi app.
Expand Down
2 changes: 1 addition & 1 deletion src/content-types/single/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { HttpClient } from '../../http';
import { BaseQueryParams, GenericDocumentResponse } from '../../types/content-api';
import { URLHelper } from '../../utilities';

const debug = createDebug('sdk:ct:single');
const debug = createDebug('strapi:ct:single');

/**
* A service class designed for interacting with a single-type resource in a Strapi app.
Expand Down
2 changes: 1 addition & 1 deletion src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './sdk';
export * from './strapi';
export * from './url';
export * from './http';
6 changes: 3 additions & 3 deletions src/errors/sdk.ts → src/errors/strapi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class StrapiSDKError extends Error {
export class StrapiError extends Error {
constructor(
cause: unknown = undefined,
message: string = 'An error occurred in the Strapi SDK. Please check the logs for more information.'
Expand All @@ -9,7 +9,7 @@ export class StrapiSDKError extends Error {
}
}

export class StrapiSDKValidationError extends StrapiSDKError {
export class StrapiValidationError extends StrapiError {
constructor(
cause: unknown = undefined,
message: string = 'Some of the provided values are not valid.'
Expand All @@ -18,7 +18,7 @@ export class StrapiSDKValidationError extends StrapiSDKError {
}
}

export class StrapiSDKInitializationError extends StrapiSDKError {
export class StrapiInitializationError extends StrapiError {
constructor(cause: unknown = undefined, message: string = 'Could not initialize the Strapi SDK') {
super(cause, message);
}
Expand Down
2 changes: 1 addition & 1 deletion src/http/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { HttpInterceptorManager } from './interceptor-manager';

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

const debug = createDebug('sdk:http');
const debug = createDebug('strapi:http');

/**
* Strapi SDK's HTTP Client
Expand Down
22 changes: 11 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StrapiSDK } from './sdk';
import { StrapiSDKValidator } from './validators';
import { Strapi } from './sdk';
import { StrapiConfigValidator } from './validators';

import type { StrapiSDKConfig } from './sdk';
import type { StrapiConfig } from './sdk';

export * from './errors';

Expand All @@ -23,7 +23,7 @@ export * from './errors';
* @example
* ```typescript
* // Basic configuration using API token auth
* const sdkConfig = {
* const config = {
* baseURL: 'https://api.example.com',
* auth: {
* strategy: 'api-token',
Expand All @@ -32,25 +32,25 @@ export * from './errors';
* };
*
* // Create the SDK instance
* const strapiSDK = strapiSDK(sdkConfig);
* const sdk = strapi(config);
*
* // Using the SDK to fetch content from a custom endpoint
* const response = await strapiSDK.fetch('/content-endpoint');
* const response = await sdk.fetch('/content-endpoint');
* const data = await response.json();
*
* console.log(data);
* ```
*
* @throws {StrapiSDKInitializationError} If the provided baseURL does not conform to a valid HTTP or HTTPS URL,
* @throws {StrapiInitializationError} If the provided baseURL doesn't conform to a valid HTTP or HTTPS URL,
* or if the auth configuration is invalid.
*/
export const strapiSDK = (config: StrapiSDKConfig) => {
const sdkValidator = new StrapiSDKValidator();
export const strapi = (config: StrapiConfig) => {
const configValidator = new StrapiConfigValidator();

return new StrapiSDK<typeof config>(
return new Strapi<typeof config>(
// Properties
config,
// Dependencies
sdkValidator
configValidator
);
};
Loading

0 comments on commit 9ac67c7

Please sign in to comment.