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,10 +61,7 @@ export * from './errors';
2561 * // Basic configuration using API token auth
2662 * const config = {
2763 * baseURL: 'https://api.example.com',
28- * auth: {
29- * strategy: 'api-token',
30- * options: { token: 'your_token_here' }
31- * }
64+ * auth: 'your_token_here',
3265 * };
3366 *
3467 * // Create the SDK instance
@@ -44,13 +77,20 @@ export * from './errors';
4477 * @throws {StrapiInitializationError } If the provided baseURL doesn't conform to a valid HTTP or HTTPS URL,
4578 * or if the auth configuration is invalid.
4679 */
47- export const strapi = ( config : StrapiConfig ) => {
48- const configValidator = new StrapiConfigValidator ( ) ;
49-
50- return new Strapi < typeof config > (
51- // Properties
52- config ,
53- // Dependencies
54- configValidator
55- ) ;
80+ export const strapi = ( config : Config ) => {
81+ const { baseURL, auth } = config ;
82+
83+ const sdkConfig : StrapiConfig = { baseURL } ;
84+
85+ // In this factory, while there is only one auth strategy available, users can't manually set the strategy options.
86+ // Since the SDK constructor needs to define a proper strategy,
87+ // it is handled here if the auth property is provided
88+ if ( auth !== undefined ) {
89+ sdkConfig . auth = {
90+ strategy : ApiTokenAuthProvider . identifier ,
91+ options : { token : auth } ,
92+ } ;
93+ }
94+
95+ return new Strapi ( sdkConfig ) ;
5696} ;
0 commit comments