Skip to content

Commit bc165c2

Browse files
committed
enhancement(sdk): simplify config and auth handling in the SDK factory
1 parent 1a228b6 commit bc165c2

File tree

4 files changed

+68
-26
lines changed

4 files changed

+68
-26
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ const sdk = strapi({
111111
// Endpoint configuration
112112
baseURL: 'http://localhost:1337/api',
113113
// Auth configuration
114-
authStrategy: 'api-token',
115114
auth: 'your-api-token-here',
116115
});
117116
```
@@ -248,7 +247,6 @@ Below is a list of available namespaces to use:
248247
| `strapi:auth:factory` | Logs the registration and creation of authentication providers. |
249248
| `strapi:auth:manager` | Logs authentication lifecycle management. |
250249
| `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. |
252250
| `strapi:ct:collection` | Logs interactions with collection-type content managers. |
253251
| `strapi:ct:single` | Logs interactions with single-type content managers. |
254252
| `strapi:utils:url-helper` | Logs URL helper utility operations (e.g., appending query parameters or formatting URLs). |

src/index.ts

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,46 @@
1+
import { ApiTokenAuthProvider } from './auth';
12
import { Strapi } from './sdk';
2-
import { StrapiConfigValidator } from './validators';
33

44
import type { StrapiConfig } from './sdk';
55

66
export * from './errors';
77

8+
export interface Config {
9+
/**
10+
* The base URL of the Strapi content API.
11+
*
12+
* This specifies where the SDK should send requests.
13+
*
14+
* The URL must include the protocol (`http` or `https`) and serve
15+
* as the root path for all later API operations.
16+
*
17+
* @example
18+
* 'https://api.example.com'
19+
*
20+
* @remarks
21+
* Failing to provide a valid HTTP or HTTPS URL results in a
22+
* `StrapiInitializationError`.
23+
*/
24+
baseURL: string;
25+
26+
/**
27+
* API token to authenticate requests (optional).
28+
*
29+
* When provided, this token is included in the `Authorization` header
30+
* of every request to the Strapi API.
31+
*
32+
* @remarks
33+
* - A valid token must be a non-empty string.
34+
*
35+
* - If the token is invalid or improperly formatted, the SDK
36+
* throws a `StrapiValidationError` during initialization.
37+
*
38+
* - If excluded, the SDK operates without authentication.
39+
*/
40+
41+
auth?: string;
42+
}
43+
844
/**
945
* Creates a new instance of the Strapi SDK with a specified configuration.
1046
*
@@ -25,8 +61,7 @@ export * from './errors';
2561
* // Basic configuration using API token auth
2662
* const config = {
2763
* baseURL: 'https://api.example.com',
28-
* authStrategy: 'api-token',
29-
* auth: 'you_token_here',
64+
* auth: 'your_token_here',
3065
* };
3166
*
3267
* // Create the SDK instance
@@ -42,13 +77,14 @@ export * from './errors';
4277
* @throws {StrapiInitializationError} If the provided baseURL doesn't conform to a valid HTTP or HTTPS URL,
4378
* or if the auth configuration is invalid.
4479
*/
45-
export const strapi = (config: StrapiConfig) => {
46-
const configValidator = new StrapiConfigValidator();
47-
48-
return new Strapi<typeof config>(
49-
// Properties
50-
config,
51-
// Dependencies
52-
configValidator
53-
);
80+
export const strapi = (config: Config) => {
81+
const customConfig: StrapiConfig = { ...config };
82+
83+
// In this factory, users can't set an authStrategy manually to keep things simple.
84+
// Since the SDK constructor needs an authStrategy if an auth property is provided, it is set to API token by default
85+
if (customConfig.auth !== undefined) {
86+
customConfig.authStrategy = ApiTokenAuthProvider.identifier;
87+
}
88+
89+
return new Strapi(customConfig);
5490
};

src/sdk.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ export interface StrapiConfig {
3030
*
3131
* It serves as the main interface through which users interact with
3232
* their Strapi installation programmatically.
33-
*
34-
* @template T_Config - Configuration type inferred from the user-provided SDK configuration
3533
*/
36-
export class Strapi<const T_Config extends StrapiConfig = StrapiConfig> {
34+
export class Strapi {
3735
/** @internal */
38-
private readonly _config: T_Config;
36+
private readonly _config: StrapiConfig;
3937

4038
/** @internal */
4139
private readonly _validator: StrapiConfigValidator;
@@ -49,7 +47,7 @@ export class Strapi<const T_Config extends StrapiConfig = StrapiConfig> {
4947
/** @internal */
5048
constructor(
5149
// Properties
52-
config: T_Config,
50+
config: StrapiConfig,
5351

5452
// Dependencies
5553
validator: StrapiConfigValidator = new StrapiConfigValidator(),

tests/unit/index.test.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { strapi, StrapiInitializationError } from '../../src';
22
import { Strapi } from '../../src/sdk';
33

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

66
describe('strapi', () => {
7-
it('should create an SDK instance with valid configuration', () => {
7+
it('should create an SDK instance with valid http configuration', () => {
88
// Arrange
9-
const config = { baseURL: 'https://api.example.com' } satisfies StrapiConfig;
9+
const config = { baseURL: 'https://api.example.com' } satisfies Config;
1010

1111
// Act
1212
const sdk = strapi(config);
@@ -16,9 +16,20 @@ describe('strapi', () => {
1616
expect(sdk).toHaveProperty('baseURL', config.baseURL);
1717
});
1818

19+
it('should create an SDK instance with valid auth configuration', () => {
20+
// Arrange
21+
const config = { baseURL: 'https://api.example.com', auth: '<token>' } satisfies Config;
22+
23+
// Act
24+
const sdk = strapi(config);
25+
26+
// Assert
27+
expect(sdk).toBeInstanceOf(Strapi);
28+
});
29+
1930
it('should throw an error for an invalid baseURL', () => {
2031
// Arrange
21-
const config = { baseURL: 'invalid-url' } satisfies StrapiConfig;
32+
const config = { baseURL: 'invalid-url' } satisfies Config;
2233

2334
// Act & Assert
2435
expect(() => strapi(config)).toThrow(StrapiInitializationError);
@@ -28,9 +39,8 @@ describe('strapi', () => {
2839
// Arrange
2940
const config = {
3041
baseURL: 'https://api.example.com',
31-
authStrategy: 'api-token',
32-
auth: '', // Invalid token
33-
} satisfies StrapiConfig;
42+
auth: '', // Invalid API token
43+
} satisfies Config;
3444

3545
// Act & Assert
3646
expect(() => strapi(config)).toThrow(StrapiInitializationError);

0 commit comments

Comments
 (0)