1+ import { ApiTokenAuthProvider } from './auth' ;
12import { Strapi } from './sdk' ;
2- import { StrapiConfigValidator } from './validators' ;
33
44import type { StrapiConfig } from './sdk' ;
55
66export * 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} ;
0 commit comments