@frontstackdev/cli 0.0.0-canary-20251023180402 → 0.0.0-canary-20251023185828

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,23 @@ import type { Query } from './query-types'
11
11
 
12
12
  type Method = 'GET' | 'POST' | 'PATCH' | 'DELETE'
13
13
 
14
+ /**
15
+ * Global configuration options for the frontstack client
16
+ */
17
+ export interface ClientConfig {
18
+ /**
19
+ * @description Proxy URL
20
+ *
21
+ * Optionally, provide a proxy URL path that will be concatenated with the original API path.
22
+ * For example, if proxyUrl is '/api' and the original request is to '/block',
23
+ * the request will be sent to '/api/block'.
24
+ * The original target URL is passed in the 'fs-target-url' header for the proxy to forward to.
25
+ * This is useful to prevent CORS issues when making requests from a browser.
26
+ * Can be overridden per-request via RequestOptions.
27
+ */
28
+ proxyUrl?: string
29
+ }
30
+
14
31
  /**
15
32
  * Interface describing the frontstack client API
16
33
  */
@@ -81,22 +98,26 @@ export interface FrontstackClient {
81
98
  * Fetch a context by its token
82
99
  * @param token The token of the context
83
100
  * @example 'ae0d4981-c363-4d5a-a49e-1f053d49f2f7'
101
+ * @param config Further configuration options
84
102
  *
85
103
  * @returns The context and token
86
104
  */
87
105
  context: (
88
- token: ContextToken
106
+ token: ContextToken,
107
+ config?: RequestOptions
89
108
  ) => Promise<Context>;
90
109
 
91
110
  /**
92
111
  * Fetch a list of contexts
93
112
  * @param token The token of the context to fetch
94
113
  * @example 'ae0d4981-c363-4d5a-a49e-1f053d49f2f7'
114
+ * @param config Further configuration options
95
115
  *
96
116
  * @returns The list of contexts and the token
97
117
  */
98
118
  contextList: (
99
- token?: ContextToken
119
+ token?: ContextToken,
120
+ config?: RequestOptions
100
121
  ) => Promise<[ContextOption[], ContextToken]>;
101
122
 
102
123
  /**
@@ -105,6 +126,7 @@ export interface FrontstackClient {
105
126
  * @example { region: 'uk', locale: 'en-GB' }
106
127
  * @param token The token of the context to update
107
128
  * @example 'ae0d4981-c363-4d5a-a49e-1f053d49f2f7'
129
+ * @param config Further configuration options
108
130
  *
109
131
  * @returns The updated context
110
132
  */
@@ -113,7 +135,8 @@ export interface FrontstackClient {
113
135
  region: string
114
136
  locale: string
115
137
  },
116
- token: ContextToken
138
+ token: ContextToken,
139
+ config?: RequestOptions
117
140
  ) => Promise<Context>;
118
141
  }
119
142
 
@@ -125,7 +148,7 @@ const endpoints: Endpoints = {
125
148
  {{/each}}
126
149
  }
127
150
 
128
- const invoke = async (path: string, method: Method, payload: any, headers: any) => {
151
+ const invoke = async (path: string, method: Method, payload: any, headers: any, proxyUrl?: string) => {
129
152
  const _headers: any = {
130
153
  'Content-Type': 'application/json',
131
154
  'Accept': 'application/json',
@@ -146,6 +169,21 @@ const invoke = async (path: string, method: Method, payload: any, headers: any)
146
169
  body: payload
147
170
  }
148
171
 
172
+ // If proxy URL is provided, concatenate it with the original path
173
+ if (proxyUrl) {
174
+ // Parse the original full URL to extract just the pathname
175
+ const url = new URL(path)
176
+ const originalPath = url.pathname
177
+
178
+ // Store the original full URL in header for the proxy to use
179
+ headers['fs-target-url'] = path
180
+
181
+ // Concatenate proxy URL with the original path
182
+ // Remove trailing slash from proxyUrl and leading slash is already in originalPath
183
+ const normalizedProxyUrl = proxyUrl.endsWith('/') ? proxyUrl.slice(0, -1) : proxyUrl
184
+ path = normalizedProxyUrl + originalPath
185
+ }
186
+
149
187
  return await ofetch.raw(path, options)
150
188
  }
151
189
 
@@ -157,124 +195,159 @@ const servers = [
157
195
  ]
158
196
 
159
197
  /**
160
- * frontstack Client
198
+ * Create a frontstack client with optional global configuration
199
+ *
200
+ * @param config Global configuration options for the client
201
+ * @example
202
+ * const client = createClient({ proxyUrl: '/api/frontstack' })
203
+ *
204
+ * @returns A configured frontstack client instance
161
205
  */
162
- const client: FrontstackClient = {
163
- /**
164
- * Fetch a block with a key
165
- */
166
- block: async (name, key, config) => {
167
- if(!(name in endpoints)) {
168
- throw new Error(`Block ${name} not found`)
169
- }
170
- let endpoint: string = `${servers[0].url}${endpoints[name]}`;
206
+ const createClient = (config?: ClientConfig): FrontstackClient => {
207
+ const globalConfig = config || {}
208
+
209
+ return {
210
+ /**
211
+ * Fetch a block with a key
212
+ */
213
+ block: async (name, key, config) => {
214
+ if(!(name in endpoints)) {
215
+ throw new Error(`Block ${name} not found`)
216
+ }
217
+ let endpoint: string = `${servers[0].url}${endpoints[name]}`;
218
+
219
+ // Replace '{key}' in the endpoint
220
+ endpoint = endpoint.replace('{key}', key);
221
+
222
+ // No additional parameters needed for blocks
223
+ let payload = { param: {} };
224
+
225
+ let headers: Record<string, string> = {}
226
+
227
+ if(config?.requestUrl !== undefined) {
228
+ headers['fs-request-url'] = config.requestUrl
229
+ }
230
+ if (config?.contextKey !== undefined) {
231
+ headers["fs-context"] = config.contextKey;
232
+ }
233
+
234
+ // Use per-request proxyUrl if provided, otherwise use global config
235
+ const proxyUrl = config?.proxyUrl ?? globalConfig.proxyUrl
236
+
237
+ return (await invoke(endpoint, 'POST', payload, headers, proxyUrl))._data
238
+ },
171
239
 
172
- // Replace '{key}' in the endpoint
173
- endpoint = endpoint.replace('{key}', key);
240
+ /**
241
+ * Fetch a listing with parameters and optional query options
242
+ */
243
+ listing: async (name, parameters, config) => {
244
+ if(!(name in endpoints)) {
245
+ throw new Error(`Listing ${name} not found`)
246
+ }
174
247
 
175
- // No additional parameters needed for blocks
176
- let payload = { param: {} };
248
+ let endpoint: string = `${servers[0].url}${endpoints[name]}`;
177
249
 
178
- let headers: Record<string, string> = {}
250
+ // Merge listing parameters with query
251
+ let payload = config && 'query' in config ? { param: {...parameters}, ...config.query } : { param: {...parameters} }
179
252
 
180
- if(config?.requestUrl !== undefined) {
181
- headers['fs-request-url'] = config.requestUrl
182
- }
183
- if (config?.contextKey !== undefined) {
184
- headers["fs-context"] = config.contextKey;
185
- }
253
+ let headers: Record<string, string> = {}
186
254
 
187
- return (await invoke(endpoint, 'POST', payload, headers))._data
188
- },
255
+ if(config?.requestUrl !== undefined) {
256
+ headers['fs-request-url'] = config.requestUrl
257
+ }
258
+ if (config?.contextKey !== undefined) {
259
+ headers["fs-context"] = config.contextKey;
260
+ }
189
261
 
190
- /**
191
- * Fetch a listing with parameters and optional query options
192
- */
193
- listing: async (name, parameters, config) => {
194
- if(!(name in endpoints)) {
195
- throw new Error(`Listing ${name} not found`)
196
- }
262
+ // Use per-request proxyUrl if provided, otherwise use global config
263
+ const proxyUrl = config?.proxyUrl ?? globalConfig.proxyUrl
197
264
 
198
- let endpoint: string = `${servers[0].url}${endpoints[name]}`;
265
+ return (await invoke(endpoint, 'POST', payload, headers, proxyUrl))._data
266
+ },
199
267
 
200
- // Merge listing parameters with query
201
- let payload = config && 'query' in config ? { param: {...parameters}, ...config.query } : { param: {...parameters} }
268
+ /**
269
+ * Fetch a page by its slug
270
+ */
271
+ page: async (slug, config) => {
272
+ let endpoint: string = `${servers[0].url}/page/${slug}`;
202
273
 
203
- let headers: Record<string, string> = {}
274
+ let headers: Record<string, string> = {}
204
275
 
205
- if(config?.requestUrl !== undefined) {
206
- headers['fs-request-url'] = config.requestUrl
207
- }
208
- if (config?.contextKey !== undefined) {
209
- headers["fs-context"] = config.contextKey;
210
- }
276
+ if(config?.requestUrl !== undefined) {
277
+ headers['fs-request-url'] = config.requestUrl
278
+ }
279
+ if (config?.contextKey !== undefined) {
280
+ headers["fs-context"] = config.contextKey;
281
+ }
211
282
 
212
- return (await invoke(endpoint, 'POST', payload, headers))._data
213
- },
283
+ // Use per-request proxyUrl if provided, otherwise use global config
284
+ const proxyUrl = config?.proxyUrl ?? globalConfig.proxyUrl
214
285
 
215
- /**
216
- * Fetch a page by its slug
217
- */
218
- page: async (slug, config) => {
219
- let endpoint: string = `${servers[0].url}/page/${slug}`;
286
+ return (await invoke(endpoint, 'GET', null, headers, proxyUrl))._data
287
+ },
220
288
 
221
- let headers: Record<string, string> = {}
289
+ /**
290
+ * Fetch a context by its token
291
+ */
292
+ context: async (token, config) => {
293
+ let endpoint: string = `${servers[0].url}/context/token`;
222
294
 
223
- if(config?.requestUrl !== undefined) {
224
- headers['fs-request-url'] = config.requestUrl
225
- }
226
- if (config?.contextKey !== undefined) {
227
- headers["fs-context"] = config.contextKey;
228
- }
295
+ let headers: Record<string, string> = {
296
+ 'fs-context': token
297
+ }
229
298
 
230
- return (await invoke(endpoint, 'GET', null, headers))._data
231
- },
299
+ // Use per-request proxyUrl if provided, otherwise use global config
300
+ const proxyUrl = config?.proxyUrl ?? globalConfig.proxyUrl
232
301
 
233
- /**
234
- * Fetch a context by its token
235
- */
236
- context: async (token) => {
237
- let endpoint: string = `${servers[0].url}/context/token`;
302
+ return (await invoke(endpoint, 'GET', null, headers, proxyUrl))._data
303
+ },
238
304
 
239
- let headers: Record<string, string> = {
240
- 'fs-context': token
241
- }
305
+ /**
306
+ * Fetch a list of contexts
307
+ */
308
+ contextList: async (token, config) => {
309
+ let endpoint: string = `${servers[0].url}/context`;
242
310
 
243
- return (await invoke(endpoint, 'GET', null, headers))._data
244
- },
311
+ const requestHeaders: Record<string, string> = {}
245
312
 
246
- /**
247
- * Fetch a list of contexts
248
- */
249
- contextList: async (token) => {
250
- let endpoint: string = `${servers[0].url}/context`;
313
+ if (token) {
314
+ requestHeaders["fs-context"] = token;
315
+ }
251
316
 
252
- const requestHeaders: Record<string, string> = {}
317
+ // Use per-request proxyUrl if provided, otherwise use global config
318
+ const proxyUrl = config?.proxyUrl ?? globalConfig.proxyUrl
253
319
 
254
- if (token) {
255
- requestHeaders["fs-context"] = token;
256
- }
320
+ const {
321
+ _data,
322
+ headers: responseHeaders
323
+ } = await invoke(endpoint, 'GET', null, requestHeaders, proxyUrl)
257
324
 
258
- const {
259
- _data,
260
- headers: responseHeaders
261
- } = await invoke(endpoint, 'GET', null, requestHeaders)
325
+ return [_data, responseHeaders.get('fs-context')!]
326
+ },
262
327
 
263
- return [_data, responseHeaders.get('fs-context')!]
264
- },
328
+ /**
329
+ * Update a context
330
+ */
331
+ contextUpdate: async (context, token, config) => {
332
+ let endpoint: string = `${servers[0].url}/context`;
265
333
 
266
- /**
267
- * Update a context
268
- */
269
- contextUpdate: async (context, token) => {
270
- let endpoint: string = `${servers[0].url}/context`;
334
+ let headers: Record<string, string> = {
335
+ 'fs-context': token
336
+ }
271
337
 
272
- let headers: Record<string, string> = {
273
- 'fs-context': token
274
- }
338
+ // Use per-request proxyUrl if provided, otherwise use global config
339
+ const proxyUrl = config?.proxyUrl ?? globalConfig.proxyUrl
275
340
 
276
- return (await invoke(endpoint, 'PATCH', context, headers))._data
341
+ return (await invoke(endpoint, 'PATCH', context, headers, proxyUrl))._data
342
+ }
277
343
  }
278
344
  }
279
345
 
346
+ /**
347
+ * Default client instance (without global configuration)
348
+ * For global configuration (e.g. proxy URL), use createClient() instead
349
+ */
350
+ const client = createClient()
351
+
280
352
  export default client
353
+ export { createClient, client }
@@ -19,6 +19,17 @@ type RequestOptions = {
19
19
  * Providing a context key will override the default context key for the request
20
20
  */
21
21
  contextKey?: string
22
+
23
+ /**
24
+ * @description Proxy URL
25
+ *
26
+ * Optionally, provide a proxy URL path that will be concatenated with the original API path.
27
+ * For example, if proxyUrl is '/api' and the original request is to '/block',
28
+ * the request will be sent to '/api/block'.
29
+ * The original target URL is passed in the 'fs-target-url' header for the proxy to forward to.
30
+ * This is useful to prevent CORS issues when making requests from a browser.
31
+ */
32
+ proxyUrl?: string
22
33
  }
23
34
 
24
35
  /* List of all types used to fetch block parameters */
package/dist/version CHANGED
@@ -1 +1 @@
1
- 0.0.0-canary-20251023180402
1
+ 0.0.0-canary-20251023185828
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frontstackdev/cli",
3
- "version": "0.0.0-canary-20251023180402",
3
+ "version": "0.0.0-canary-20251023185828",
4
4
  "description": "frontstack CLI for managing projects",
5
5
  "type": "module",
6
6
  "module": "dist/frontstack.mjs",