@tumbaland/frontend-core 1.1.1 → 1.2.0

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.
@@ -7,6 +7,8 @@ export interface ApiClientOptions {
7
7
  export interface ApiRequestOptions extends RequestInit {
8
8
  /** Skip the onUnauthorized callback for this call (e.g. an auth-check that expects 401 as a normal "not logged in" result, not a hard redirect). */
9
9
  skipAuthRedirect?: boolean;
10
+ /** Serialized onto the URL as a query string; undefined/null values are omitted. */
11
+ params?: Record<string, string | number | boolean | undefined | null>;
10
12
  }
11
13
  export declare class ApiError extends Error {
12
14
  status: number;
@@ -18,9 +20,7 @@ export declare class ApiError extends Error {
18
20
  * correlation/session headers, structured errors, and an opt-in 401 →
19
21
  * redirect-to-auth hook, per TECHNICAL_REVIEW.md P1.3. `authService` and
20
22
  * `groupService` don't route through this: they use different auth
21
- * transports (cookie vs. Bearer token) that predate this client. New
22
- * call sites (the many per-front album/finance/etc. service files that
23
- * still hand-roll fetch) can adopt this incrementally.
23
+ * transports (cookie vs. Bearer token) that predate this client.
24
24
  */
25
25
  export declare function createApiClient(options: ApiClientOptions): {
26
26
  request: <T = unknown>(path: string, init?: ApiRequestOptions) => Promise<T>;
package/dist/apiClient.js CHANGED
@@ -1,4 +1,18 @@
1
1
  import { getCorrelationId, getSessionId } from '@tumbaland/components';
2
+ function appendParams(path, params) {
3
+ if (!params)
4
+ return path;
5
+ const searchParams = new URLSearchParams();
6
+ Object.entries(params).forEach(([key, value]) => {
7
+ if (value !== undefined && value !== null) {
8
+ searchParams.append(key, String(value));
9
+ }
10
+ });
11
+ const query = searchParams.toString();
12
+ if (!query)
13
+ return path;
14
+ return `${path}${path.includes('?') ? '&' : '?'}${query}`;
15
+ }
2
16
  export class ApiError extends Error {
3
17
  constructor(status, message, body) {
4
18
  super(message);
@@ -12,14 +26,12 @@ export class ApiError extends Error {
12
26
  * correlation/session headers, structured errors, and an opt-in 401 →
13
27
  * redirect-to-auth hook, per TECHNICAL_REVIEW.md P1.3. `authService` and
14
28
  * `groupService` don't route through this: they use different auth
15
- * transports (cookie vs. Bearer token) that predate this client. New
16
- * call sites (the many per-front album/finance/etc. service files that
17
- * still hand-roll fetch) can adopt this incrementally.
29
+ * transports (cookie vs. Bearer token) that predate this client.
18
30
  */
19
31
  export function createApiClient(options) {
20
32
  async function request(path, init = {}) {
21
- const { skipAuthRedirect, headers, ...rest } = init;
22
- const response = await fetch(`${options.baseUrl()}${path}`, {
33
+ const { skipAuthRedirect, headers, params, ...rest } = init;
34
+ const response = await fetch(`${options.baseUrl()}${appendParams(path, params)}`, {
23
35
  credentials: 'include',
24
36
  ...rest,
25
37
  headers: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tumbaland/frontend-core",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "Shared frontend auth/group/API-client logic for Tumbaland frontends",
5
5
  "author": "Tumbaland",
6
6
  "license": "MIT",