@classytic/arc-next 0.1.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.
- package/LICENSE +21 -0
- package/README.md +185 -0
- package/dist/api.d.ts +225 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +167 -0
- package/dist/api.js.map +1 -0
- package/dist/client.d.ts +172 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +242 -0
- package/dist/client.js.map +1 -0
- package/dist/hooks.d.ts +143 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +357 -0
- package/dist/hooks.js.map +1 -0
- package/dist/mutation.d.ts +118 -0
- package/dist/mutation.d.ts.map +1 -0
- package/dist/mutation.js +160 -0
- package/dist/mutation.js.map +1 -0
- package/dist/prefetch.d.ts +64 -0
- package/dist/prefetch.d.ts.map +1 -0
- package/dist/prefetch.js +76 -0
- package/dist/prefetch.js.map +1 -0
- package/dist/query-client.d.ts +25 -0
- package/dist/query-client.d.ts.map +1 -0
- package/dist/query-client.js +46 -0
- package/dist/query-client.js.map +1 -0
- package/dist/query.d.ts +177 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +203 -0
- package/dist/query.js.map +1 -0
- package/package.json +91 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Classytic
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# @classytic/arc-next
|
|
2
|
+
|
|
3
|
+
React + TanStack Query SDK for Arc resources. Production-grade CRUD hooks with optimistic updates, multi-tenant scoping, and pluggable configuration.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @classytic/arc-next
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**Peer dependencies:**
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install react@^19 @tanstack/react-query@^5
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Setup
|
|
18
|
+
|
|
19
|
+
Call the configuration functions once at app init (e.g., in your root providers):
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { configureClient } from "@classytic/arc-next/client";
|
|
23
|
+
import { configureToast } from "@classytic/arc-next/mutation";
|
|
24
|
+
import { configureNavigation } from "@classytic/arc-next/hooks";
|
|
25
|
+
import { toast } from "sonner";
|
|
26
|
+
import { useRouter } from "next/navigation";
|
|
27
|
+
|
|
28
|
+
// Required — sets the API base URL
|
|
29
|
+
configureClient({
|
|
30
|
+
baseUrl: process.env.NEXT_PUBLIC_API_URL!,
|
|
31
|
+
internalApiKey: process.env.NEXT_PUBLIC_INTERNAL_API_KEY, // optional
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Optional — pluggable toast (defaults to console)
|
|
35
|
+
configureToast({
|
|
36
|
+
success: toast.success,
|
|
37
|
+
error: toast.error,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Optional — enables useNavigation() routing (defaults to cache-only)
|
|
41
|
+
configureNavigation(useRouter);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
### 1. Define your API
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
import { createCrudApi } from "@classytic/arc-next/api";
|
|
50
|
+
|
|
51
|
+
interface Product {
|
|
52
|
+
_id: string;
|
|
53
|
+
name: string;
|
|
54
|
+
price: number;
|
|
55
|
+
organizationId: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface CreateProduct {
|
|
59
|
+
name: string;
|
|
60
|
+
price: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export const productsApi = createCrudApi<Product, CreateProduct>(
|
|
64
|
+
"products",
|
|
65
|
+
{ basePath: "/api" }
|
|
66
|
+
);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### 2. Create hooks
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { createCrudHooks } from "@classytic/arc-next/hooks";
|
|
73
|
+
import { productsApi } from "./products-api";
|
|
74
|
+
|
|
75
|
+
export const {
|
|
76
|
+
KEYS: productKeys,
|
|
77
|
+
cache: productCache,
|
|
78
|
+
useList: useProducts,
|
|
79
|
+
useDetail: useProduct,
|
|
80
|
+
useActions: useProductActions,
|
|
81
|
+
useNavigation: useProductNavigation,
|
|
82
|
+
} = createCrudHooks<Product, CreateProduct>({
|
|
83
|
+
api: productsApi,
|
|
84
|
+
entityKey: "products",
|
|
85
|
+
singular: "Product",
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### 3. Use in components
|
|
90
|
+
|
|
91
|
+
```tsx
|
|
92
|
+
"use client";
|
|
93
|
+
|
|
94
|
+
export function ProductsPage() {
|
|
95
|
+
const { items, pagination, isLoading } = useProducts(null, {
|
|
96
|
+
organizationId: "org-123",
|
|
97
|
+
}, { public: true });
|
|
98
|
+
|
|
99
|
+
const { create, remove, isCreating } = useProductActions();
|
|
100
|
+
|
|
101
|
+
const handleCreate = async () => {
|
|
102
|
+
await create({ data: { name: "New Product", price: 29.99 } });
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
if (isLoading) return <div>Loading...</div>;
|
|
106
|
+
|
|
107
|
+
return (
|
|
108
|
+
<div>
|
|
109
|
+
<button onClick={handleCreate} disabled={isCreating}>
|
|
110
|
+
Add Product
|
|
111
|
+
</button>
|
|
112
|
+
{items.map((product) => (
|
|
113
|
+
<div key={product._id}>
|
|
114
|
+
{product.name} — ${product.price}
|
|
115
|
+
<button onClick={() => remove({ id: product._id })}>Delete</button>
|
|
116
|
+
</div>
|
|
117
|
+
))}
|
|
118
|
+
{pagination && <span>{pagination.total} total</span>}
|
|
119
|
+
</div>
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Subpath Exports
|
|
125
|
+
|
|
126
|
+
| Import | Purpose | `"use client"` |
|
|
127
|
+
|---|---|:-:|
|
|
128
|
+
| `@classytic/arc-next/client` | `configureClient`, `handleApiRequest`, `createQueryString` | No |
|
|
129
|
+
| `@classytic/arc-next/api` | `BaseApi`, `createCrudApi`, response types, type guards | No |
|
|
130
|
+
| `@classytic/arc-next/query` | `createQueryKeys`, `createCacheUtils`, `createListQuery`, `createDetailQuery` | Yes |
|
|
131
|
+
| `@classytic/arc-next/mutation` | `configureToast`, `useMutationWithTransition`, `createOptimisticMutation` | Yes |
|
|
132
|
+
| `@classytic/arc-next/hooks` | `createCrudHooks`, `configureNavigation` | Yes |
|
|
133
|
+
| `@classytic/arc-next/query-client` | `getQueryClient` (SSR-safe singleton) | No |
|
|
134
|
+
|
|
135
|
+
## Features
|
|
136
|
+
|
|
137
|
+
- **CRUD Factory** — `createCrudApi` + `createCrudHooks` generates typed API clients and React Query hooks
|
|
138
|
+
- **Optimistic Updates** — Create, update, delete with instant UI feedback and automatic rollback
|
|
139
|
+
- **Multi-Tenant Scoping** — `organizationId` in headers + scoped query keys
|
|
140
|
+
- **Pagination Normalization** — Handles `docs`/`data`/`items` response formats, offset/keyset/aggregate pagination
|
|
141
|
+
- **Detail Cache Prefilling** — List results auto-populate detail query cache
|
|
142
|
+
- **React 19 Transitions** — `useMutationWithTransition` wraps invalidation in `startTransition`
|
|
143
|
+
- **Pluggable Toast** — `configureToast()` — use sonner, react-hot-toast, or anything
|
|
144
|
+
- **Pluggable Navigation** — `configureNavigation()` — use Next.js, React Router, or any router
|
|
145
|
+
- **SSR-Safe QueryClient** — `getQueryClient()` — singleton in browser, new per request on server
|
|
146
|
+
- **Framework-Agnostic** — No hard dependency on Next.js
|
|
147
|
+
- **Tree-Shakeable** — `sideEffects: false`, flat files, no barrels
|
|
148
|
+
|
|
149
|
+
## Custom Mutations
|
|
150
|
+
|
|
151
|
+
For operations beyond CRUD (publish, schedule, upload), use the mutation factories directly:
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
import { useMutationWithTransition } from "@classytic/arc-next/mutation";
|
|
155
|
+
import { productsApi, productKeys } from "./products";
|
|
156
|
+
|
|
157
|
+
export function usePublishProduct() {
|
|
158
|
+
return useMutationWithTransition({
|
|
159
|
+
mutationFn: (id: string) =>
|
|
160
|
+
productsApi.request("POST", `${productsApi.baseUrl}/${id}/publish`),
|
|
161
|
+
invalidateQueries: [productKeys.all],
|
|
162
|
+
messages: { success: "Product published!", error: "Failed to publish" },
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## QueryClient Setup
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
import { getQueryClient } from "@classytic/arc-next/query-client";
|
|
171
|
+
import { QueryClientProvider } from "@tanstack/react-query";
|
|
172
|
+
|
|
173
|
+
function Providers({ children }) {
|
|
174
|
+
const queryClient = getQueryClient();
|
|
175
|
+
return (
|
|
176
|
+
<QueryClientProvider client={queryClient}>
|
|
177
|
+
{children}
|
|
178
|
+
</QueryClientProvider>
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## License
|
|
184
|
+
|
|
185
|
+
MIT
|
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { ArcClient } from "./client.js";
|
|
2
|
+
|
|
3
|
+
//#region src/api.d.ts
|
|
4
|
+
interface PopulateOption {
|
|
5
|
+
path: string;
|
|
6
|
+
select?: string;
|
|
7
|
+
match?: Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
interface ApiResponse<T = unknown> {
|
|
10
|
+
success: boolean;
|
|
11
|
+
data?: T;
|
|
12
|
+
message?: string;
|
|
13
|
+
}
|
|
14
|
+
interface OffsetPaginationResponse<T = unknown> {
|
|
15
|
+
success: boolean;
|
|
16
|
+
method: 'offset';
|
|
17
|
+
docs: T[];
|
|
18
|
+
page: number;
|
|
19
|
+
limit: number;
|
|
20
|
+
total: number;
|
|
21
|
+
pages: number;
|
|
22
|
+
hasNext: boolean;
|
|
23
|
+
hasPrev: boolean;
|
|
24
|
+
warning?: string;
|
|
25
|
+
}
|
|
26
|
+
interface KeysetPaginationResponse<T = unknown> {
|
|
27
|
+
success: boolean;
|
|
28
|
+
method: 'keyset';
|
|
29
|
+
docs: T[];
|
|
30
|
+
limit: number;
|
|
31
|
+
hasMore: boolean;
|
|
32
|
+
next: string | null;
|
|
33
|
+
}
|
|
34
|
+
interface AggregatePaginationResponse<T = unknown> {
|
|
35
|
+
success: boolean;
|
|
36
|
+
method: 'aggregate';
|
|
37
|
+
docs: T[];
|
|
38
|
+
page: number;
|
|
39
|
+
limit: number;
|
|
40
|
+
total: number;
|
|
41
|
+
pages: number;
|
|
42
|
+
hasNext: boolean;
|
|
43
|
+
hasPrev: boolean;
|
|
44
|
+
warning?: string;
|
|
45
|
+
}
|
|
46
|
+
type PaginatedResponse<T = unknown> = OffsetPaginationResponse<T> | KeysetPaginationResponse<T> | AggregatePaginationResponse<T>;
|
|
47
|
+
interface DeleteResponse {
|
|
48
|
+
success: boolean;
|
|
49
|
+
deleted: boolean;
|
|
50
|
+
id?: string;
|
|
51
|
+
soft?: boolean;
|
|
52
|
+
message?: string;
|
|
53
|
+
count?: number;
|
|
54
|
+
}
|
|
55
|
+
type SortDirection = 1 | -1 | 'asc' | 'desc';
|
|
56
|
+
type SortSpec = Record<string, SortDirection> | string;
|
|
57
|
+
type FilterOperator = 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte' | 'in' | 'nin' | 'contains' | 'startsWith' | 'endsWith' | 'regex';
|
|
58
|
+
interface QueryParams {
|
|
59
|
+
page?: number;
|
|
60
|
+
limit?: number;
|
|
61
|
+
after?: string;
|
|
62
|
+
cursor?: string;
|
|
63
|
+
sort?: string;
|
|
64
|
+
select?: string;
|
|
65
|
+
populate?: string | string[];
|
|
66
|
+
populateOptions?: PopulateOption[];
|
|
67
|
+
lean?: boolean | 'true' | 'false';
|
|
68
|
+
[key: string]: unknown;
|
|
69
|
+
}
|
|
70
|
+
interface RequestOptions {
|
|
71
|
+
token?: string | null;
|
|
72
|
+
organizationId?: string | null;
|
|
73
|
+
cache?: RequestCache;
|
|
74
|
+
revalidate?: number;
|
|
75
|
+
tags?: string[];
|
|
76
|
+
headerOptions?: Record<string, string>;
|
|
77
|
+
responseType?: 'json' | 'blob' | 'text';
|
|
78
|
+
signal?: AbortSignal;
|
|
79
|
+
}
|
|
80
|
+
interface BaseApiConfig {
|
|
81
|
+
basePath?: string;
|
|
82
|
+
defaultParams?: {
|
|
83
|
+
limit?: number;
|
|
84
|
+
page?: number;
|
|
85
|
+
[key: string]: unknown;
|
|
86
|
+
};
|
|
87
|
+
cache?: RequestCache;
|
|
88
|
+
headers?: Record<string, string>;
|
|
89
|
+
client?: ArcClient;
|
|
90
|
+
}
|
|
91
|
+
declare class BaseApi<TDoc = Record<string, unknown>, TCreate = Partial<TDoc>, TUpdate = Partial<TDoc>> {
|
|
92
|
+
readonly entity: string;
|
|
93
|
+
readonly config: Required<Omit<BaseApiConfig, 'client'>>;
|
|
94
|
+
readonly baseUrl: string;
|
|
95
|
+
private readonly requestFn;
|
|
96
|
+
constructor(entity: string, config?: BaseApiConfig);
|
|
97
|
+
createQueryString(params?: Record<string, unknown>): string;
|
|
98
|
+
prepareParams(params?: QueryParams): Record<string, unknown>;
|
|
99
|
+
getAll({
|
|
100
|
+
token,
|
|
101
|
+
organizationId,
|
|
102
|
+
params,
|
|
103
|
+
options
|
|
104
|
+
}?: {
|
|
105
|
+
token?: string | null;
|
|
106
|
+
organizationId?: string | null;
|
|
107
|
+
params?: QueryParams;
|
|
108
|
+
options?: Omit<RequestOptions, 'token' | 'organizationId'>;
|
|
109
|
+
}): Promise<PaginatedResponse<TDoc>>;
|
|
110
|
+
getById({
|
|
111
|
+
token,
|
|
112
|
+
organizationId,
|
|
113
|
+
id,
|
|
114
|
+
params,
|
|
115
|
+
options
|
|
116
|
+
}: {
|
|
117
|
+
token?: string | null;
|
|
118
|
+
organizationId?: string | null;
|
|
119
|
+
id: string;
|
|
120
|
+
params?: {
|
|
121
|
+
select?: string;
|
|
122
|
+
populate?: string | string[];
|
|
123
|
+
};
|
|
124
|
+
options?: Omit<RequestOptions, 'token' | 'organizationId'>;
|
|
125
|
+
}): Promise<ApiResponse<TDoc>>;
|
|
126
|
+
create({
|
|
127
|
+
token,
|
|
128
|
+
organizationId,
|
|
129
|
+
data,
|
|
130
|
+
options
|
|
131
|
+
}: {
|
|
132
|
+
token?: string | null;
|
|
133
|
+
organizationId?: string | null;
|
|
134
|
+
data: TCreate;
|
|
135
|
+
options?: Omit<RequestOptions, 'token' | 'organizationId'>;
|
|
136
|
+
}): Promise<ApiResponse<TDoc>>;
|
|
137
|
+
update({
|
|
138
|
+
token,
|
|
139
|
+
organizationId,
|
|
140
|
+
id,
|
|
141
|
+
data,
|
|
142
|
+
options
|
|
143
|
+
}: {
|
|
144
|
+
token?: string | null;
|
|
145
|
+
organizationId?: string | null;
|
|
146
|
+
id: string;
|
|
147
|
+
data: TUpdate;
|
|
148
|
+
options?: Omit<RequestOptions, 'token' | 'organizationId'>;
|
|
149
|
+
}): Promise<ApiResponse<TDoc>>;
|
|
150
|
+
delete({
|
|
151
|
+
token,
|
|
152
|
+
organizationId,
|
|
153
|
+
id,
|
|
154
|
+
options
|
|
155
|
+
}: {
|
|
156
|
+
token?: string | null;
|
|
157
|
+
organizationId?: string | null;
|
|
158
|
+
id: string;
|
|
159
|
+
options?: Omit<RequestOptions, 'token' | 'organizationId'>;
|
|
160
|
+
}): Promise<DeleteResponse>;
|
|
161
|
+
upload({
|
|
162
|
+
token,
|
|
163
|
+
organizationId,
|
|
164
|
+
data,
|
|
165
|
+
path,
|
|
166
|
+
options
|
|
167
|
+
}: {
|
|
168
|
+
token?: string | null;
|
|
169
|
+
organizationId?: string | null;
|
|
170
|
+
data: FormData;
|
|
171
|
+
path?: string;
|
|
172
|
+
options?: Omit<RequestOptions, 'token' | 'organizationId'>;
|
|
173
|
+
}): Promise<ApiResponse<TDoc>>;
|
|
174
|
+
search({
|
|
175
|
+
token,
|
|
176
|
+
organizationId,
|
|
177
|
+
searchParams,
|
|
178
|
+
params,
|
|
179
|
+
options
|
|
180
|
+
}?: {
|
|
181
|
+
token?: string | null;
|
|
182
|
+
organizationId?: string | null;
|
|
183
|
+
searchParams?: Record<string, unknown>;
|
|
184
|
+
params?: QueryParams;
|
|
185
|
+
options?: Omit<RequestOptions, 'token' | 'organizationId'>;
|
|
186
|
+
}): Promise<PaginatedResponse<TDoc>>;
|
|
187
|
+
findBy({
|
|
188
|
+
token,
|
|
189
|
+
organizationId,
|
|
190
|
+
field,
|
|
191
|
+
value,
|
|
192
|
+
operator,
|
|
193
|
+
params,
|
|
194
|
+
options
|
|
195
|
+
}: {
|
|
196
|
+
token?: string | null;
|
|
197
|
+
organizationId?: string | null;
|
|
198
|
+
field: string;
|
|
199
|
+
value: unknown;
|
|
200
|
+
operator?: FilterOperator;
|
|
201
|
+
params?: QueryParams;
|
|
202
|
+
options?: Omit<RequestOptions, 'token' | 'organizationId'>;
|
|
203
|
+
}): Promise<PaginatedResponse<TDoc>>;
|
|
204
|
+
request<TResponse = unknown>(method: 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE', endpoint: string, {
|
|
205
|
+
token,
|
|
206
|
+
organizationId,
|
|
207
|
+
data,
|
|
208
|
+
params,
|
|
209
|
+
options
|
|
210
|
+
}?: {
|
|
211
|
+
token?: string;
|
|
212
|
+
organizationId?: string | null;
|
|
213
|
+
data?: unknown;
|
|
214
|
+
params?: QueryParams;
|
|
215
|
+
options?: Omit<RequestOptions, 'token' | 'organizationId'>;
|
|
216
|
+
}): Promise<TResponse>;
|
|
217
|
+
}
|
|
218
|
+
declare function createCrudApi<TDoc = Record<string, unknown>, TCreate = Partial<TDoc>, TUpdate = Partial<TDoc>>(entity: string, config?: BaseApiConfig): BaseApi<TDoc, TCreate, TUpdate>;
|
|
219
|
+
type ExtractDoc<T> = T extends PaginatedResponse<infer D> ? D : never;
|
|
220
|
+
declare function isOffsetPagination<T>(response: PaginatedResponse<T>): response is OffsetPaginationResponse<T>;
|
|
221
|
+
declare function isKeysetPagination<T>(response: PaginatedResponse<T>): response is KeysetPaginationResponse<T>;
|
|
222
|
+
declare function isAggregatePagination<T>(response: PaginatedResponse<T>): response is AggregatePaginationResponse<T>;
|
|
223
|
+
//#endregion
|
|
224
|
+
export { AggregatePaginationResponse, ApiResponse, BaseApi, BaseApiConfig, DeleteResponse, ExtractDoc, FilterOperator, KeysetPaginationResponse, OffsetPaginationResponse, PaginatedResponse, PopulateOption, QueryParams, RequestOptions, SortDirection, SortSpec, createCrudApi, isAggregatePagination, isKeysetPagination, isOffsetPagination };
|
|
225
|
+
//# sourceMappingURL=api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.d.ts","names":[],"sources":["../src/api.ts"],"mappings":";;;UAOiB,cAAA;EACf,IAAA;EACA,MAAA;EACA,KAAA,GAAQ,MAAA;AAAA;AAAA,UAOO,WAAA;EACf,OAAA;EACA,IAAA,GAAO,CAAA;EACP,OAAA;AAAA;AAAA,UAGe,wBAAA;EACf,OAAA;EACA,MAAA;EACA,IAAA,EAAM,CAAA;EACN,IAAA;EACA,KAAA;EACA,KAAA;EACA,KAAA;EACA,OAAA;EACA,OAAA;EACA,OAAA;AAAA;AAAA,UAGe,wBAAA;EACf,OAAA;EACA,MAAA;EACA,IAAA,EAAM,CAAA;EACN,KAAA;EACA,OAAA;EACA,IAAA;AAAA;AAAA,UAGe,2BAAA;EACf,OAAA;EACA,MAAA;EACA,IAAA,EAAM,CAAA;EACN,IAAA;EACA,KAAA;EACA,KAAA;EACA,KAAA;EACA,OAAA;EACA,OAAA;EACA,OAAA;AAAA;AAAA,KAGU,iBAAA,gBACR,wBAAA,CAAyB,CAAA,IACzB,wBAAA,CAAyB,CAAA,IACzB,2BAAA,CAA4B,CAAA;AAAA,UAEf,cAAA;EACf,OAAA;EACA,OAAA;EACA,EAAA;EACA,IAAA;EACA,OAAA;EACA,KAAA;AAAA;AAAA,KAOU,aAAA;AAAA,KACA,QAAA,GAAW,MAAA,SAAe,aAAA;AAAA,KAE1B,cAAA;AAAA,UAKK,WAAA;EACf,IAAA;EACA,KAAA;EACA,KAAA;EACA,MAAA;EACA,IAAA;EACA,MAAA;EACA,QAAA;EACA,eAAA,GAAkB,cAAA;EAClB,IAAA;EAAA,CACC,GAAA;AAAA;AAAA,UAGc,cAAA;EACf,KAAA;EACA,cAAA;EACA,KAAA,GAAQ,YAAA;EACR,UAAA;EACA,IAAA;EACA,aAAA,GAAgB,MAAA;EAChB,YAAA;EACA,MAAA,GAAS,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,CAAQ,IAAA,EAAA,CAClB,MAAA,UAAgB,MAAA,GAAQ,aAAA,GAAqB,OAAA,CAAQ,IAAA,EAAM,OAAA,EAAS,OAAA;AAAA,KAQ1D,UAAA,MAAgB,CAAA,SAAU,iBAAA,YAA6B,CAAA;AAAA,iBAEnD,kBAAA,GAAA,CACd,QAAA,EAAU,iBAAA,CAAkB,CAAA,IAC3B,QAAA,IAAY,wBAAA,CAAyB,CAAA;AAAA,iBAIxB,kBAAA,GAAA,CACd,QAAA,EAAU,iBAAA,CAAkB,CAAA,IAC3B,QAAA,IAAY,wBAAA,CAAyB,CAAA;AAAA,iBAIxB,qBAAA,GAAA,CACd,QAAA,EAAU,iBAAA,CAAkB,CAAA,IAC3B,QAAA,IAAY,2BAAA,CAA4B,CAAA"}
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { createQueryString, handleApiRequest } from "./client.js";
|
|
2
|
+
|
|
3
|
+
//#region src/api.ts
|
|
4
|
+
var BaseApi = class {
|
|
5
|
+
entity;
|
|
6
|
+
config;
|
|
7
|
+
baseUrl;
|
|
8
|
+
requestFn;
|
|
9
|
+
constructor(entity, config = {}) {
|
|
10
|
+
this.entity = entity;
|
|
11
|
+
this.requestFn = config.client?.request ?? handleApiRequest;
|
|
12
|
+
this.config = {
|
|
13
|
+
basePath: config.basePath ?? "/api/v1",
|
|
14
|
+
defaultParams: {
|
|
15
|
+
limit: 10,
|
|
16
|
+
page: 1,
|
|
17
|
+
...config.defaultParams || {}
|
|
18
|
+
},
|
|
19
|
+
cache: config.cache ?? "no-store",
|
|
20
|
+
headers: { ...config.headers || {} }
|
|
21
|
+
};
|
|
22
|
+
this.baseUrl = `${this.config.basePath}/${this.entity}`;
|
|
23
|
+
}
|
|
24
|
+
createQueryString(params = {}) {
|
|
25
|
+
return createQueryString(params);
|
|
26
|
+
}
|
|
27
|
+
prepareParams(params = {}) {
|
|
28
|
+
const result = {};
|
|
29
|
+
const CRITICAL_FILTERS = ["organizationId", "ownerId"];
|
|
30
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
31
|
+
if (CRITICAL_FILTERS.includes(key)) {
|
|
32
|
+
result[key] = value || null;
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (key === "populateOptions") {
|
|
36
|
+
if (Array.isArray(value) && value.length > 0) result[key] = value;
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (value !== void 0 && value !== "") if (["page", "limit"].includes(key)) result[key] = parseInt(String(value)) || (key === "page" ? 1 : 10);
|
|
40
|
+
else if (Array.isArray(value)) {
|
|
41
|
+
if (value.length > 1) result[`${key}[in]`] = value.join(",");
|
|
42
|
+
else if (value.length === 1) result[key] = value[0];
|
|
43
|
+
} else result[key] = value;
|
|
44
|
+
});
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
async getAll({ token = null, organizationId = null, params = {}, options = {} } = {}) {
|
|
48
|
+
const processedParams = this.prepareParams(params);
|
|
49
|
+
const queryString = this.createQueryString(processedParams);
|
|
50
|
+
const requestOptions = {
|
|
51
|
+
cache: this.config.cache,
|
|
52
|
+
...options
|
|
53
|
+
};
|
|
54
|
+
if (token) requestOptions.token = token;
|
|
55
|
+
if (organizationId) requestOptions.organizationId = organizationId;
|
|
56
|
+
return this.requestFn("GET", `${this.baseUrl}?${queryString}`, requestOptions);
|
|
57
|
+
}
|
|
58
|
+
async getById({ token = null, organizationId = null, id, params = {}, options = {} }) {
|
|
59
|
+
if (!id) throw new Error("ID is required");
|
|
60
|
+
const queryString = this.createQueryString(params);
|
|
61
|
+
const url = queryString ? `${this.baseUrl}/${id}?${queryString}` : `${this.baseUrl}/${id}`;
|
|
62
|
+
const requestOptions = {
|
|
63
|
+
cache: this.config.cache,
|
|
64
|
+
...options
|
|
65
|
+
};
|
|
66
|
+
if (token) requestOptions.token = token;
|
|
67
|
+
if (organizationId) requestOptions.organizationId = organizationId;
|
|
68
|
+
return this.requestFn("GET", url, requestOptions);
|
|
69
|
+
}
|
|
70
|
+
async create({ token, organizationId = null, data, options = {} }) {
|
|
71
|
+
const requestOptions = {
|
|
72
|
+
body: data,
|
|
73
|
+
...options
|
|
74
|
+
};
|
|
75
|
+
if (token) requestOptions.token = token;
|
|
76
|
+
if (organizationId) requestOptions.organizationId = organizationId;
|
|
77
|
+
return this.requestFn("POST", this.baseUrl, requestOptions);
|
|
78
|
+
}
|
|
79
|
+
async update({ token, organizationId = null, id, data, options = {} }) {
|
|
80
|
+
if (!id) throw new Error("ID is required");
|
|
81
|
+
const requestOptions = {
|
|
82
|
+
body: data,
|
|
83
|
+
...options
|
|
84
|
+
};
|
|
85
|
+
if (token) requestOptions.token = token;
|
|
86
|
+
if (organizationId) requestOptions.organizationId = organizationId;
|
|
87
|
+
return this.requestFn("PATCH", `${this.baseUrl}/${id}`, requestOptions);
|
|
88
|
+
}
|
|
89
|
+
async delete({ token, organizationId = null, id, options = {} }) {
|
|
90
|
+
if (!id) throw new Error("ID is required");
|
|
91
|
+
const requestOptions = { ...options };
|
|
92
|
+
if (token) requestOptions.token = token;
|
|
93
|
+
if (organizationId) requestOptions.organizationId = organizationId;
|
|
94
|
+
return this.requestFn("DELETE", `${this.baseUrl}/${id}`, requestOptions);
|
|
95
|
+
}
|
|
96
|
+
async upload({ token, organizationId = null, data, path, options = {} }) {
|
|
97
|
+
const url = path ? `${this.baseUrl}/${path}` : this.baseUrl;
|
|
98
|
+
const requestOptions = {
|
|
99
|
+
body: data,
|
|
100
|
+
...options
|
|
101
|
+
};
|
|
102
|
+
if (token) requestOptions.token = token;
|
|
103
|
+
if (organizationId) requestOptions.organizationId = organizationId;
|
|
104
|
+
return this.requestFn("POST", url, requestOptions);
|
|
105
|
+
}
|
|
106
|
+
async search({ token = null, organizationId = null, searchParams = {}, params = {}, options = {} } = {}) {
|
|
107
|
+
const queryParams = {
|
|
108
|
+
...params,
|
|
109
|
+
...searchParams
|
|
110
|
+
};
|
|
111
|
+
const processedParams = this.prepareParams(queryParams);
|
|
112
|
+
const queryString = this.createQueryString(processedParams);
|
|
113
|
+
const requestOptions = {
|
|
114
|
+
cache: this.config.cache,
|
|
115
|
+
...options
|
|
116
|
+
};
|
|
117
|
+
if (token) requestOptions.token = token;
|
|
118
|
+
if (organizationId) requestOptions.organizationId = organizationId;
|
|
119
|
+
return this.requestFn("GET", `${this.baseUrl}?${queryString}`, requestOptions);
|
|
120
|
+
}
|
|
121
|
+
async findBy({ token = null, organizationId = null, field, value, operator, params = {}, options = {} }) {
|
|
122
|
+
if (!field || value === void 0) throw new Error("Field and value are required");
|
|
123
|
+
const queryParams = { ...params };
|
|
124
|
+
if (operator) queryParams[`${field}[${operator}]`] = Array.isArray(value) ? value.join(",") : value;
|
|
125
|
+
else queryParams[field] = value;
|
|
126
|
+
const processedParams = this.prepareParams(queryParams);
|
|
127
|
+
const queryString = this.createQueryString(processedParams);
|
|
128
|
+
const requestOptions = {
|
|
129
|
+
cache: this.config.cache,
|
|
130
|
+
...options
|
|
131
|
+
};
|
|
132
|
+
if (token) requestOptions.token = token;
|
|
133
|
+
if (organizationId) requestOptions.organizationId = organizationId;
|
|
134
|
+
return this.requestFn("GET", `${this.baseUrl}?${queryString}`, requestOptions);
|
|
135
|
+
}
|
|
136
|
+
async request(method, endpoint, { token, organizationId = null, data, params, options = {} } = {}) {
|
|
137
|
+
let url = endpoint;
|
|
138
|
+
if (params) {
|
|
139
|
+
const processedParams = this.prepareParams(params);
|
|
140
|
+
url = `${endpoint}?${this.createQueryString(processedParams)}`;
|
|
141
|
+
}
|
|
142
|
+
const requestOptions = {
|
|
143
|
+
body: data,
|
|
144
|
+
cache: this.config.cache,
|
|
145
|
+
...options
|
|
146
|
+
};
|
|
147
|
+
if (token) requestOptions.token = token;
|
|
148
|
+
if (organizationId) requestOptions.organizationId = organizationId;
|
|
149
|
+
return this.requestFn(method, url, requestOptions);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
function createCrudApi(entity, config = {}) {
|
|
153
|
+
return new BaseApi(entity, config);
|
|
154
|
+
}
|
|
155
|
+
function isOffsetPagination(response) {
|
|
156
|
+
return response.method === "offset";
|
|
157
|
+
}
|
|
158
|
+
function isKeysetPagination(response) {
|
|
159
|
+
return response.method === "keyset";
|
|
160
|
+
}
|
|
161
|
+
function isAggregatePagination(response) {
|
|
162
|
+
return response.method === "aggregate";
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
//#endregion
|
|
166
|
+
export { BaseApi, createCrudApi, isAggregatePagination, isKeysetPagination, isOffsetPagination };
|
|
167
|
+
//# sourceMappingURL=api.js.map
|
package/dist/api.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.js","names":[],"sources":["../src/api.ts"],"sourcesContent":["import { handleApiRequest, createQueryString } from './client.js';\nimport type { ApiRequestOptions, ArcClient } from './client.js';\n\n// ============================================================================\n// Populate Types\n// ============================================================================\n\nexport interface PopulateOption {\n path: string;\n select?: string;\n match?: Record<string, unknown>;\n}\n\n// ============================================================================\n// Response Types\n// ============================================================================\n\nexport interface ApiResponse<T = unknown> {\n success: boolean;\n data?: T;\n message?: string;\n}\n\nexport interface OffsetPaginationResponse<T = unknown> {\n success: boolean;\n method: 'offset';\n docs: T[];\n page: number;\n limit: number;\n total: number;\n pages: number;\n hasNext: boolean;\n hasPrev: boolean;\n warning?: string;\n}\n\nexport interface KeysetPaginationResponse<T = unknown> {\n success: boolean;\n method: 'keyset';\n docs: T[];\n limit: number;\n hasMore: boolean;\n next: string | null;\n}\n\nexport interface AggregatePaginationResponse<T = unknown> {\n success: boolean;\n method: 'aggregate';\n docs: T[];\n page: number;\n limit: number;\n total: number;\n pages: number;\n hasNext: boolean;\n hasPrev: boolean;\n warning?: string;\n}\n\nexport type PaginatedResponse<T = unknown> =\n | OffsetPaginationResponse<T>\n | KeysetPaginationResponse<T>\n | AggregatePaginationResponse<T>;\n\nexport interface DeleteResponse {\n success: boolean;\n deleted: boolean;\n id?: string;\n soft?: boolean;\n message?: string;\n count?: number;\n}\n\n// ============================================================================\n// Request Types\n// ============================================================================\n\nexport type SortDirection = 1 | -1 | 'asc' | 'desc';\nexport type SortSpec = Record<string, SortDirection> | string;\n\nexport type FilterOperator =\n | 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte'\n | 'in' | 'nin'\n | 'contains' | 'startsWith' | 'endsWith' | 'regex';\n\nexport interface QueryParams {\n page?: number;\n limit?: number;\n after?: string;\n cursor?: string;\n sort?: string;\n select?: string;\n populate?: string | string[];\n populateOptions?: PopulateOption[];\n lean?: boolean | 'true' | 'false';\n [key: string]: unknown;\n}\n\nexport interface RequestOptions {\n token?: string | null;\n organizationId?: string | null;\n cache?: RequestCache;\n revalidate?: number;\n tags?: string[];\n headerOptions?: Record<string, string>;\n responseType?: 'json' | 'blob' | 'text';\n signal?: AbortSignal;\n}\n\nexport interface BaseApiConfig {\n basePath?: string;\n defaultParams?: {\n limit?: number;\n page?: number;\n [key: string]: unknown;\n };\n cache?: RequestCache;\n headers?: Record<string, string>;\n client?: ArcClient;\n}\n\n// ============================================================================\n// Request Function Type\n// ============================================================================\n\ntype RequestFn = <T = unknown>(\n method: 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE',\n endpoint: string,\n options?: ApiRequestOptions,\n) => Promise<T>;\n\n// ============================================================================\n// Base API Class\n// ============================================================================\n\nexport class BaseApi<\n TDoc = Record<string, unknown>,\n TCreate = Partial<TDoc>,\n TUpdate = Partial<TDoc>\n> {\n readonly entity: string;\n readonly config: Required<Omit<BaseApiConfig, 'client'>>;\n readonly baseUrl: string;\n private readonly requestFn: RequestFn;\n\n constructor(entity: string, config: BaseApiConfig = {}) {\n this.entity = entity;\n this.requestFn = config.client?.request ?? handleApiRequest;\n this.config = {\n basePath: config.basePath ?? '/api/v1',\n defaultParams: {\n limit: 10,\n page: 1,\n ...(config.defaultParams || {}),\n },\n cache: config.cache ?? 'no-store',\n headers: {\n ...(config.headers || {}),\n },\n };\n\n this.baseUrl = `${this.config.basePath}/${this.entity}`;\n }\n\n createQueryString(params: Record<string, unknown> = {}): string {\n return createQueryString(params);\n }\n\n prepareParams(params: QueryParams = {}): Record<string, unknown> {\n const result: Record<string, unknown> = {};\n const CRITICAL_FILTERS = ['organizationId', 'ownerId'];\n\n Object.entries(params).forEach(([key, value]) => {\n if (CRITICAL_FILTERS.includes(key)) {\n result[key] = value || null;\n return;\n }\n\n if (key === 'populateOptions') {\n if (Array.isArray(value) && value.length > 0) {\n result[key] = value;\n }\n return;\n }\n\n if (value !== undefined && value !== '') {\n if (['page', 'limit'].includes(key)) {\n result[key] = parseInt(String(value)) || (key === 'page' ? 1 : 10);\n } else if (Array.isArray(value)) {\n if (value.length > 1) {\n result[`${key}[in]`] = value.join(',');\n } else if (value.length === 1) {\n result[key] = value[0];\n }\n } else {\n result[key] = value;\n }\n }\n });\n\n return result;\n }\n\n async getAll({\n token = null,\n organizationId = null,\n params = {},\n options = {},\n }: {\n token?: string | null;\n organizationId?: string | null;\n params?: QueryParams;\n options?: Omit<RequestOptions, 'token' | 'organizationId'>;\n } = {}): Promise<PaginatedResponse<TDoc>> {\n const processedParams = this.prepareParams(params);\n const queryString = this.createQueryString(processedParams);\n\n const requestOptions: ApiRequestOptions = {\n cache: this.config.cache,\n ...options,\n };\n\n if (token) requestOptions.token = token;\n if (organizationId) requestOptions.organizationId = organizationId;\n\n return this.requestFn('GET', `${this.baseUrl}?${queryString}`, requestOptions);\n }\n\n async getById({\n token = null,\n organizationId = null,\n id,\n params = {},\n options = {},\n }: {\n token?: string | null;\n organizationId?: string | null;\n id: string;\n params?: { select?: string; populate?: string | string[] };\n options?: Omit<RequestOptions, 'token' | 'organizationId'>;\n }): Promise<ApiResponse<TDoc>> {\n if (!id) throw new Error('ID is required');\n\n const queryString = this.createQueryString(params);\n const url = queryString ? `${this.baseUrl}/${id}?${queryString}` : `${this.baseUrl}/${id}`;\n\n const requestOptions: ApiRequestOptions = {\n cache: this.config.cache,\n ...options,\n };\n\n if (token) requestOptions.token = token;\n if (organizationId) requestOptions.organizationId = organizationId;\n\n return this.requestFn('GET', url, requestOptions);\n }\n\n async create({\n token,\n organizationId = null,\n data,\n options = {},\n }: {\n token?: string | null;\n organizationId?: string | null;\n data: TCreate;\n options?: Omit<RequestOptions, 'token' | 'organizationId'>;\n }): Promise<ApiResponse<TDoc>> {\n const requestOptions: ApiRequestOptions = {\n body: data,\n ...options,\n };\n\n if (token) requestOptions.token = token;\n if (organizationId) requestOptions.organizationId = organizationId;\n\n return this.requestFn('POST', this.baseUrl, requestOptions);\n }\n\n async update({\n token,\n organizationId = null,\n id,\n data,\n options = {},\n }: {\n token?: string | null;\n organizationId?: string | null;\n id: string;\n data: TUpdate;\n options?: Omit<RequestOptions, 'token' | 'organizationId'>;\n }): Promise<ApiResponse<TDoc>> {\n if (!id) throw new Error('ID is required');\n\n const requestOptions: ApiRequestOptions = {\n body: data,\n ...options,\n };\n\n if (token) requestOptions.token = token;\n if (organizationId) requestOptions.organizationId = organizationId;\n\n return this.requestFn('PATCH', `${this.baseUrl}/${id}`, requestOptions);\n }\n\n async delete({\n token,\n organizationId = null,\n id,\n options = {},\n }: {\n token?: string | null;\n organizationId?: string | null;\n id: string;\n options?: Omit<RequestOptions, 'token' | 'organizationId'>;\n }): Promise<DeleteResponse> {\n if (!id) throw new Error('ID is required');\n\n const requestOptions: ApiRequestOptions = { ...options };\n\n if (token) requestOptions.token = token;\n if (organizationId) requestOptions.organizationId = organizationId;\n\n return this.requestFn('DELETE', `${this.baseUrl}/${id}`, requestOptions);\n }\n\n async upload({\n token,\n organizationId = null,\n data,\n path,\n options = {},\n }: {\n token?: string | null;\n organizationId?: string | null;\n data: FormData;\n path?: string;\n options?: Omit<RequestOptions, 'token' | 'organizationId'>;\n }): Promise<ApiResponse<TDoc>> {\n const url = path ? `${this.baseUrl}/${path}` : this.baseUrl;\n\n const requestOptions: ApiRequestOptions = {\n body: data,\n ...options,\n };\n\n if (token) requestOptions.token = token;\n if (organizationId) requestOptions.organizationId = organizationId;\n\n return this.requestFn('POST', url, requestOptions);\n }\n\n async search({\n token = null,\n organizationId = null,\n searchParams = {},\n params = {},\n options = {},\n }: {\n token?: string | null;\n organizationId?: string | null;\n searchParams?: Record<string, unknown>;\n params?: QueryParams;\n options?: Omit<RequestOptions, 'token' | 'organizationId'>;\n } = {}): Promise<PaginatedResponse<TDoc>> {\n const queryParams = { ...params, ...searchParams };\n const processedParams = this.prepareParams(queryParams);\n const queryString = this.createQueryString(processedParams);\n\n const requestOptions: ApiRequestOptions = {\n cache: this.config.cache,\n ...options,\n };\n\n if (token) requestOptions.token = token;\n if (organizationId) requestOptions.organizationId = organizationId;\n\n return this.requestFn('GET', `${this.baseUrl}?${queryString}`, requestOptions);\n }\n\n async findBy({\n token = null,\n organizationId = null,\n field,\n value,\n operator,\n params = {},\n options = {},\n }: {\n token?: string | null;\n organizationId?: string | null;\n field: string;\n value: unknown;\n operator?: FilterOperator;\n params?: QueryParams;\n options?: Omit<RequestOptions, 'token' | 'organizationId'>;\n }): Promise<PaginatedResponse<TDoc>> {\n if (!field || value === undefined) {\n throw new Error('Field and value are required');\n }\n\n const queryParams: QueryParams = { ...params };\n\n if (operator) {\n queryParams[`${field}[${operator}]`] = Array.isArray(value) ? value.join(',') : value;\n } else {\n queryParams[field] = value;\n }\n\n const processedParams = this.prepareParams(queryParams);\n const queryString = this.createQueryString(processedParams);\n\n const requestOptions: ApiRequestOptions = {\n cache: this.config.cache,\n ...options,\n };\n\n if (token) requestOptions.token = token;\n if (organizationId) requestOptions.organizationId = organizationId;\n\n return this.requestFn('GET', `${this.baseUrl}?${queryString}`, requestOptions);\n }\n\n async request<TResponse = unknown>(\n method: 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE',\n endpoint: string,\n {\n token,\n organizationId = null,\n data,\n params,\n options = {},\n }: {\n token?: string;\n organizationId?: string | null;\n data?: unknown;\n params?: QueryParams;\n options?: Omit<RequestOptions, 'token' | 'organizationId'>;\n } = {}\n ): Promise<TResponse> {\n let url = endpoint;\n\n if (params) {\n const processedParams = this.prepareParams(params);\n const queryString = this.createQueryString(processedParams);\n url = `${endpoint}?${queryString}`;\n }\n\n const requestOptions: ApiRequestOptions = {\n body: data,\n cache: this.config.cache,\n ...options,\n };\n\n if (token) requestOptions.token = token;\n if (organizationId) requestOptions.organizationId = organizationId;\n\n return this.requestFn(method, url, requestOptions);\n }\n}\n\n// ============================================================================\n// Factory\n// ============================================================================\n\nexport function createCrudApi<\n TDoc = Record<string, unknown>,\n TCreate = Partial<TDoc>,\n TUpdate = Partial<TDoc>\n>(entity: string, config: BaseApiConfig = {}): BaseApi<TDoc, TCreate, TUpdate> {\n return new BaseApi<TDoc, TCreate, TUpdate>(entity, config);\n}\n\n// ============================================================================\n// Type Helpers\n// ============================================================================\n\nexport type ExtractDoc<T> = T extends PaginatedResponse<infer D> ? D : never;\n\nexport function isOffsetPagination<T>(\n response: PaginatedResponse<T>\n): response is OffsetPaginationResponse<T> {\n return response.method === 'offset';\n}\n\nexport function isKeysetPagination<T>(\n response: PaginatedResponse<T>\n): response is KeysetPaginationResponse<T> {\n return response.method === 'keyset';\n}\n\nexport function isAggregatePagination<T>(\n response: PaginatedResponse<T>\n): response is AggregatePaginationResponse<T> {\n return response.method === 'aggregate';\n}\n"],"mappings":";;;AAsIA,IAAa,UAAb,MAIE;CACA,AAAS;CACT,AAAS;CACT,AAAS;CACT,AAAiB;CAEjB,YAAY,QAAgB,SAAwB,EAAE,EAAE;AACtD,OAAK,SAAS;AACd,OAAK,YAAY,OAAO,QAAQ,WAAW;AAC3C,OAAK,SAAS;GACZ,UAAU,OAAO,YAAY;GAC7B,eAAe;IACb,OAAO;IACP,MAAM;IACN,GAAI,OAAO,iBAAiB,EAAE;IAC/B;GACD,OAAO,OAAO,SAAS;GACvB,SAAS,EACP,GAAI,OAAO,WAAW,EAAE,EACzB;GACF;AAED,OAAK,UAAU,GAAG,KAAK,OAAO,SAAS,GAAG,KAAK;;CAGjD,kBAAkB,SAAkC,EAAE,E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