@contello/client 3.0.2 → 3.1.1

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/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
+ import "undici";
1
2
  import { ReadableStream as ReadableStream$1 } from "node:stream/web";
2
-
3
3
  //#region src/http.d.ts
4
4
  type HttpAgentOptions = {
5
5
  pipelining?: number | undefined;
@@ -23,15 +23,17 @@ type ProxyResult = {
23
23
  type OperationKind = 'query' | 'mutation' | 'subscription';
24
24
  type OperationDef<TResult = unknown, TVariables = Record<string, unknown>, TKind extends OperationKind = OperationKind> = {
25
25
  document: string;
26
- kind: TKind; /** phantom type — exists only at the type level */
27
- __result?: TResult | undefined; /** phantom type — exists only at the type level */
26
+ kind: TKind;
27
+ /** phantom type — exists only at the type level */
28
+ __result?: TResult | undefined;
29
+ /** phantom type — exists only at the type level */
28
30
  __variables?: TVariables | undefined;
29
31
  };
30
32
  type OperationMap = Record<string, OperationDef>;
31
33
  type InferResult<T extends OperationDef> = NonNullable<T['__result']>;
32
34
  type InferVariables<T extends OperationDef> = NonNullable<T['__variables']>;
33
35
  type RpcMethod<T extends OperationDef> = T['kind'] extends 'subscription' ? InferVariables<T> extends Record<string, never> ? () => AsyncIterable<InferResult<T>> : (variables: InferVariables<T>) => AsyncIterable<InferResult<T>> : InferVariables<T> extends Record<string, never> ? () => Promise<InferResult<T>> : (variables: InferVariables<T>) => Promise<InferResult<T>>;
34
- type Rpc<T extends OperationMap> = { [K in keyof T]: RpcMethod<T[K]> };
36
+ type Rpc<T extends OperationMap> = { [K in keyof T]: RpcMethod<T[K]>; };
35
37
  /**
36
38
  * Discriminator for how a SourceDef binds to a Subscription field:
37
39
  * - `entity` — a Contello entity model collection (`categoriesBatch` etc.), accepts optional `{ ids }` filter
@@ -41,13 +43,72 @@ type Rpc<T extends OperationMap> = { [K in keyof T]: RpcMethod<T[K]> };
41
43
  * - `i18nMessage` — built-in: `contelloI18nMessagesBatch`, requires `{ collection }` and optional `{ ids }` filter
42
44
  */
43
45
  type SourceCardinality = 'entity' | 'singleton' | 'route' | 'asset' | 'i18nMessage';
44
- type SourceDef<TModel extends string = string, TCardinality extends SourceCardinality = SourceCardinality, TResult = unknown> = {
45
- /** the GQL fragment text — bundled with transitive deps if any */document: string; /** the fragment name to spread inside the source subscription — e.g. `Category` */
46
- fragment: string; /** the subscription field this fragment is bound to — `categoriesBatch`, `config`, etc */
46
+ /**
47
+ * One argument of a write mutation and what fills it. Entity mutations take a single input
48
+ * object; the built-ins vary — `createContelloRoute` takes `route:`, `deleteContelloRoute`
49
+ * takes a bare `id:`.
50
+ */
51
+ type SourceMutationArgument = {
52
+ /** the GQL argument name — `request`, `route`, `id` */
53
+ name: string;
54
+ /** the rendered GQL type, including nullability — `CreateCategoryRequestInput!`, `String` */
55
+ type: string;
56
+ /** `input` takes the caller's input, `id` the id passed to a delete */
57
+ from: 'input' | 'id';
58
+ /**
59
+ * the single field of a one-field envelope argument (`CreateCategoryRequestInput { entity }`).
60
+ * When set, the accessor takes the inner input and the runtime nests it under this key.
61
+ */
62
+ envelope?: string | undefined;
63
+ };
64
+ /**
65
+ * What a write mutation answers with:
66
+ * - `entity` — the entity itself, selected through the source's fragment
67
+ * - `idObject` — an object carrying `id` (`ContelloEntityDeleteResponse`)
68
+ * - `idScalar` — a bare id (`deleteContelloRoute: String!`)
69
+ */
70
+ type SourceMutationResult = 'entity' | 'idObject' | 'idScalar';
71
+ /**
72
+ * How one write operation of a source binds to a Mutation field. Emitted by the generator
73
+ * from introspection — the runtime never derives field or argument names by string munging.
74
+ */
75
+ type SourceMutationBinding = {
76
+ /** the mutation field — `createCategory` */
77
+ field: string;
78
+ arguments: SourceMutationArgument[];
79
+ result: SourceMutationResult;
80
+ };
81
+ /** Mutation bindings of a source. Absent operations are simply missing (singletons have no `create`). */
82
+ type SourceMutations = {
83
+ create?: SourceMutationBinding | undefined;
84
+ update?: SourceMutationBinding | undefined;
85
+ delete?: SourceMutationBinding | undefined;
86
+ };
87
+ /**
88
+ * Phantom shape carrying the input types of a source's write operations. A key is present
89
+ * only when the schema exposes the matching mutation, so `client.sources.x.create` exists
90
+ * exactly when `createX` does.
91
+ */
92
+ type SourceWrites = {
93
+ create?: unknown;
94
+ update?: unknown;
95
+ delete?: unknown;
96
+ };
97
+ type SourceDef<TModel extends string = string, TCardinality extends SourceCardinality = SourceCardinality, TResult = unknown, TWrites = unknown> = {
98
+ /** the GQL fragment text — bundled with transitive deps if any */
99
+ document: string;
100
+ /** the fragment name to spread inside the source subscription — e.g. `Category` */
101
+ fragment: string;
102
+ /** the subscription field this fragment is bound to — `categoriesBatch`, `config`, etc */
47
103
  subscription: string;
104
+ /** write bindings — present only for sources whose model exposes entity mutations */
105
+ mutations?: SourceMutations | undefined;
48
106
  __model: TModel;
49
- __cardinality: TCardinality; /** phantom type — exists only at the type level */
107
+ __cardinality: TCardinality;
108
+ /** phantom type — exists only at the type level */
50
109
  __result?: TResult | undefined;
110
+ /** phantom type — exists only at the type level */
111
+ __writes?: TWrites | undefined;
51
112
  };
52
113
  type SourceMap = Record<string, SourceDef>;
53
114
  /**
@@ -72,10 +133,33 @@ type SourceFetcherFor<S> = S extends SourceDef<string, 'singleton', infer R> ? (
72
133
  collection: string;
73
134
  ids?: string[] | undefined;
74
135
  }) => Promise<R[]> : never;
136
+ /** Extracts the phantom write-input shape of a SourceDef. */
137
+ type WritesOf<S> = S extends SourceDef<string, SourceCardinality, any, infer W> ? W : unknown;
138
+ /** Extracts the phantom result shape of a SourceDef — what its fragment selects. */
139
+ type ResultOf<S> = S extends SourceDef<string, SourceCardinality, infer R, any> ? R : unknown;
140
+ /**
141
+ * Per-source write accessors. Each method exists only when the generator found the matching
142
+ * mutation. A `create`/`update` selects the source's own fragment, so it answers with the entity
143
+ * in exactly the shape `fetch` yields; a `delete` answers with the id it removed. Nothing is
144
+ * cached here — that is the job of the collection that owns the source in `@contello/store`.
145
+ */
146
+ type SourceWritersFor<S> = (WritesOf<S> extends {
147
+ create: infer TCreate;
148
+ } ? {
149
+ create: (input: TCreate) => Promise<ResultOf<S>>;
150
+ } : Record<never, never>) & (WritesOf<S> extends {
151
+ update: infer TUpdate;
152
+ } ? {
153
+ update: (input: TUpdate) => Promise<ResultOf<S>>;
154
+ } : Record<never, never>) & (WritesOf<S> extends {
155
+ delete: infer TDelete;
156
+ } ? {
157
+ delete: (input: TDelete) => Promise<string>;
158
+ } : Record<never, never>);
75
159
  /** Per-source runtime accessor: `client.sources.category.fetch()` → `Promise<CategoryFragment[]>` etc. */
76
160
  type SourceAccessors<TSources extends SourceMap> = { [K in keyof TSources]: {
77
161
  fetch: SourceFetcherFor<TSources[K]>;
78
- } };
162
+ } & SourceWritersFor<TSources[K]>; };
79
163
  //#endregion
80
164
  //#region src/upload.d.ts
81
165
  type UploadMetadata = {
@@ -162,5 +246,39 @@ type LocalDate = {
162
246
  //#region src/source-subscription.d.ts
163
247
  declare function createSourceSubscription(source: SourceDef): string;
164
248
  //#endregion
165
- export { type ConnectionContext, type ConnectionEvents, ContelloClient, type ContelloClientOptions, type DownloadResult, type HttpAgentOptions, type LocalDate, type LocalDateTime, type OperationDef, type OperationKind, type OperationMap, type ProxyResult, type Rpc, type Schema, type SourceAccessors, type SourceCardinality, type SourceDef, type SourceMap, type UploadData, type UploadMetadata, type UploadOptions, createContelloClient, createSourceSubscription };
249
+ //#region src/source-mutation.d.ts
250
+ type SourceMutationKind = keyof SourceMutations;
251
+ /** What a write takes: the caller's input, and the id for a delete. */
252
+ type SourceMutationValues = {
253
+ input?: unknown;
254
+ id?: string | undefined;
255
+ };
256
+ /** The mutation document for one write operation of a source. Cached per source + kind. */
257
+ declare function createSourceMutation(source: SourceDef, kind: SourceMutationKind): string;
258
+ /**
259
+ * Variables for a write: one per bound argument, the input nested under the envelope field when
260
+ * the argument is a one-field wrapper, and everything encoded on the way out (LocalDate /
261
+ * LocalDateTime structs become wire strings) — every caller goes through here, so no write path
262
+ * can skip the encoding.
263
+ */
264
+ declare function createSourceMutationVariables(binding: SourceMutationBinding, values: SourceMutationValues): Record<string, unknown>;
265
+ /** Pulls the id out of whatever shape the mutation answered with. */
266
+ declare function readSourceMutationId(binding: SourceMutationBinding, result: unknown): string;
267
+ //#endregion
268
+ //#region src/built-in-mutations.d.ts
269
+ /**
270
+ * Write bindings for the built-in sources. Unlike entity mutations these are not derived — their
271
+ * field and argument names are fixed parts of every Contello schema — but the generator still
272
+ * checks each one against the introspected schema before emitting it, so a server without them
273
+ * (or with a different shape) simply yields a read-only source.
274
+ *
275
+ * Routes are the odd ones: `updateContelloRoute` is keyed by the `path` inside its input rather
276
+ * than by an id, and it replaces the route rather than patching it. `deleteContelloRoute` takes a
277
+ * bare `id` and answers with a bare id string.
278
+ *
279
+ * Assets have no `create` — an asset comes into being through `client.upload()`, not a mutation.
280
+ */
281
+ declare const BUILT_IN_MUTATIONS: Partial<Record<SourceCardinality, SourceMutations>>;
282
+ //#endregion
283
+ export { BUILT_IN_MUTATIONS, type ConnectionContext, type ConnectionEvents, ContelloClient, type ContelloClientOptions, type DownloadResult, type HttpAgentOptions, type LocalDate, type LocalDateTime, type OperationDef, type OperationKind, type OperationMap, type ProxyResult, type Rpc, type Schema, type SourceAccessors, type SourceCardinality, type SourceDef, type SourceMap, type SourceMutationArgument, type SourceMutationBinding, type SourceMutationKind, type SourceMutationResult, type SourceMutationValues, type SourceMutations, type SourceWrites, type UploadData, type UploadMetadata, type UploadOptions, createContelloClient, createSourceMutation, createSourceMutationVariables, createSourceSubscription, readSourceMutationId };
166
284
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","names":[],"sources":["../src/http.ts","../src/types.ts","../src/upload.ts","../src/client.ts","../src/scalars.ts","../src/source-subscription.ts"],"mappings":";;;KAiCY,gBAAA;EACV,UAAA;EACA,OAAA;EACA,oBAAA;EACA,gBAAA;AAAA;AAAA,KAcU,cAAA;EACV,QAAA;EACA,IAAA;EACA,KAA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types.ts","../src/upload.ts","../src/client.ts","../src/scalars.ts","../src/source-subscription.ts","../src/source-mutation.ts","../src/built-in-mutations.ts"],"mappings":";;;KAiCY;EACV;EACA;EACA;EACA;;KAcU;EACV;EACA;EACA,SAAS,QAAQ;EACjB,UAAU,eAAe;;KAiCf;EACV;EACA,SAAS;EACT,UAAU,eAAe;;;;KC3Ff;KAEA,aACV,mBACA,aAAa,yBACb,cAAc,gBAAgB;EAE9B;EACA,MAAM;;EAEN,WAAW;;EAEX,cAAc;;KAGJ,eAAe,eAAe;KAE9B,YAAY,UAAU,gBAAgB,YAAY;KAClD,eAAe,UAAU,gBAAgB,YAAY;KAErD,UAAU,UAAU,gBAAgB,mCAC5C,eAAe,WAAW,8BAClB,cAAc,YAAY,OAC/B,WAAW,eAAe,OAAO,cAAc,YAAY,MAC9D,eAAe,WAAW,8BAClB,QAAQ,YAAY,OACzB,WAAW,eAAe,OAAO,QAAQ,YAAY;KAEhD,IAAI,UAAU,mBACvB,WAAW,IAAI,UAAU,EAAE;;;;;;;;;KAWlB;;;;;;KAOA;;EAEV;;EAEA;;EAEA;;;;;EAKA;;;;;;;;KASU;;;;;KAMA;;EAEV;EACA,WAAW;EACX,QAAQ;;;KAIE;EACV,SAAS;EACT,SAAS;EACT,SAAS;;;;;;;KAQC;EACV;EACA;EACA;;KAGU,UACV,gCACA,qBAAqB,oBAAoB,mBACzC,mBACA;;EAGA;;EAEA;;EAEA;;EAEA,YAAY;EACZ,SAAS;EACT,eAAe;;EAEf,WAAW;;EAEX,WAAW;;KAGD,YAAY,eAAe;;;;;;;KAQ3B,OACV,aAAa,eAAe,cAC5B,iBAAiB,YAAY,WAC7B;EAEA,YAAY;EACZ,SAAS;EACT,QAAQ,OAAO;;KAGZ,iBAAiB,KACpB,UAAU,qCAAqC,WACrC,QAAQ,KACd,UAAU,kCAAkC,MACzC;EAAS;MAAqB,QAAQ,OACvC,UAAU,iCAAiC,MACxC;EAAS;EAA4B;kBAA+C,QAAQ,OAC7F,U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F,QAAQ,OAAO,eAAe,YAAY,sCAAsC,QAAQ;;iBAS1E,qBAAqB,gBAAgB,gCACnD,SAAS,sBAAsB,WAC9B,eAAe;;;KChSN;EACV;EACA;EACA;EACA;EACA;EACA;;KAGU;EACV;EACA;EACA;;;;iBCcc,yBAAyB,QAAQ;;;KCvBrC,2BAA2B;;KAG3B;EACV;EACA;;;iBAwBc,qBAAqB,QAAQ,WAAW,MAAM;;;;;;;iBA+B9C,8BACd,SAAS,uBACT,QAAQ,uBACP;;iBAYa,qBAAqB,SAAS,uBAAuB;;;;;;;;;;;;;;;cChExD,oBAAoB,QAAQ,OAAO,mBAAmB"}
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { Agent } from "undici";
2
2
  import { ReadableStream as ReadableStream$1 } from "node:stream/web";
3
-
4
3
  //#region src/http.d.ts
5
4
  type HttpAgentOptions = {
6
5
  pipelining?: number | undefined;
@@ -24,15 +23,17 @@ type ProxyResult = {
24
23
  type OperationKind = 'query' | 'mutation' | 'subscription';
25
24
  type OperationDef<TResult = unknown, TVariables = Record<string, unknown>, TKind extends OperationKind = OperationKind> = {
26
25
  document: string;
27
- kind: TKind; /** phantom type — exists only at the type level */
28
- __result?: TResult | undefined; /** phantom type — exists only at the type level */
26
+ kind: TKind;
27
+ /** phantom type — exists only at the type level */
28
+ __result?: TResult | undefined;
29
+ /** phantom type — exists only at the type level */
29
30
  __variables?: TVariables | undefined;
30
31
  };
31
32
  type OperationMap = Record<string, OperationDef>;
32
33
  type InferResult<T extends OperationDef> = NonNullable<T['__result']>;
33
34
  type InferVariables<T extends OperationDef> = NonNullable<T['__variables']>;
34
35
  type RpcMethod<T extends OperationDef> = T['kind'] extends 'subscription' ? InferVariables<T> extends Record<string, never> ? () => AsyncIterable<InferResult<T>> : (variables: InferVariables<T>) => AsyncIterable<InferResult<T>> : InferVariables<T> extends Record<string, never> ? () => Promise<InferResult<T>> : (variables: InferVariables<T>) => Promise<InferResult<T>>;
35
- type Rpc<T extends OperationMap> = { [K in keyof T]: RpcMethod<T[K]> };
36
+ type Rpc<T extends OperationMap> = { [K in keyof T]: RpcMethod<T[K]>; };
36
37
  /**
37
38
  * Discriminator for how a SourceDef binds to a Subscription field:
38
39
  * - `entity` — a Contello entity model collection (`categoriesBatch` etc.), accepts optional `{ ids }` filter
@@ -42,13 +43,72 @@ type Rpc<T extends OperationMap> = { [K in keyof T]: RpcMethod<T[K]> };
42
43
  * - `i18nMessage` — built-in: `contelloI18nMessagesBatch`, requires `{ collection }` and optional `{ ids }` filter
43
44
  */
44
45
  type SourceCardinality = 'entity' | 'singleton' | 'route' | 'asset' | 'i18nMessage';
45
- type SourceDef<TModel extends string = string, TCardinality extends SourceCardinality = SourceCardinality, TResult = unknown> = {
46
- /** the GQL fragment text — bundled with transitive deps if any */document: string; /** the fragment name to spread inside the source subscription — e.g. `Category` */
47
- fragment: string; /** the subscription field this fragment is bound to — `categoriesBatch`, `config`, etc */
46
+ /**
47
+ * One argument of a write mutation and what fills it. Entity mutations take a single input
48
+ * object; the built-ins vary — `createContelloRoute` takes `route:`, `deleteContelloRoute`
49
+ * takes a bare `id:`.
50
+ */
51
+ type SourceMutationArgument = {
52
+ /** the GQL argument name — `request`, `route`, `id` */
53
+ name: string;
54
+ /** the rendered GQL type, including nullability — `CreateCategoryRequestInput!`, `String` */
55
+ type: string;
56
+ /** `input` takes the caller's input, `id` the id passed to a delete */
57
+ from: 'input' | 'id';
58
+ /**
59
+ * the single field of a one-field envelope argument (`CreateCategoryRequestInput { entity }`).
60
+ * When set, the accessor takes the inner input and the runtime nests it under this key.
61
+ */
62
+ envelope?: string | undefined;
63
+ };
64
+ /**
65
+ * What a write mutation answers with:
66
+ * - `entity` — the entity itself, selected through the source's fragment
67
+ * - `idObject` — an object carrying `id` (`ContelloEntityDeleteResponse`)
68
+ * - `idScalar` — a bare id (`deleteContelloRoute: String!`)
69
+ */
70
+ type SourceMutationResult = 'entity' | 'idObject' | 'idScalar';
71
+ /**
72
+ * How one write operation of a source binds to a Mutation field. Emitted by the generator
73
+ * from introspection — the runtime never derives field or argument names by string munging.
74
+ */
75
+ type SourceMutationBinding = {
76
+ /** the mutation field — `createCategory` */
77
+ field: string;
78
+ arguments: SourceMutationArgument[];
79
+ result: SourceMutationResult;
80
+ };
81
+ /** Mutation bindings of a source. Absent operations are simply missing (singletons have no `create`). */
82
+ type SourceMutations = {
83
+ create?: SourceMutationBinding | undefined;
84
+ update?: SourceMutationBinding | undefined;
85
+ delete?: SourceMutationBinding | undefined;
86
+ };
87
+ /**
88
+ * Phantom shape carrying the input types of a source's write operations. A key is present
89
+ * only when the schema exposes the matching mutation, so `client.sources.x.create` exists
90
+ * exactly when `createX` does.
91
+ */
92
+ type SourceWrites = {
93
+ create?: unknown;
94
+ update?: unknown;
95
+ delete?: unknown;
96
+ };
97
+ type SourceDef<TModel extends string = string, TCardinality extends SourceCardinality = SourceCardinality, TResult = unknown, TWrites = unknown> = {
98
+ /** the GQL fragment text — bundled with transitive deps if any */
99
+ document: string;
100
+ /** the fragment name to spread inside the source subscription — e.g. `Category` */
101
+ fragment: string;
102
+ /** the subscription field this fragment is bound to — `categoriesBatch`, `config`, etc */
48
103
  subscription: string;
104
+ /** write bindings — present only for sources whose model exposes entity mutations */
105
+ mutations?: SourceMutations | undefined;
49
106
  __model: TModel;
50
- __cardinality: TCardinality; /** phantom type — exists only at the type level */
107
+ __cardinality: TCardinality;
108
+ /** phantom type — exists only at the type level */
51
109
  __result?: TResult | undefined;
110
+ /** phantom type — exists only at the type level */
111
+ __writes?: TWrites | undefined;
52
112
  };
53
113
  type SourceMap = Record<string, SourceDef>;
54
114
  /**
@@ -73,10 +133,33 @@ type SourceFetcherFor<S> = S extends SourceDef<string, 'singleton', infer R> ? (
73
133
  collection: string;
74
134
  ids?: string[] | undefined;
75
135
  }) => Promise<R[]> : never;
136
+ /** Extracts the phantom write-input shape of a SourceDef. */
137
+ type WritesOf<S> = S extends SourceDef<string, SourceCardinality, any, infer W> ? W : unknown;
138
+ /** Extracts the phantom result shape of a SourceDef — what its fragment selects. */
139
+ type ResultOf<S> = S extends SourceDef<string, SourceCardinality, infer R, any> ? R : unknown;
140
+ /**
141
+ * Per-source write accessors. Each method exists only when the generator found the matching
142
+ * mutation. A `create`/`update` selects the source's own fragment, so it answers with the entity
143
+ * in exactly the shape `fetch` yields; a `delete` answers with the id it removed. Nothing is
144
+ * cached here — that is the job of the collection that owns the source in `@contello/store`.
145
+ */
146
+ type SourceWritersFor<S> = (WritesOf<S> extends {
147
+ create: infer TCreate;
148
+ } ? {
149
+ create: (input: TCreate) => Promise<ResultOf<S>>;
150
+ } : Record<never, never>) & (WritesOf<S> extends {
151
+ update: infer TUpdate;
152
+ } ? {
153
+ update: (input: TUpdate) => Promise<ResultOf<S>>;
154
+ } : Record<never, never>) & (WritesOf<S> extends {
155
+ delete: infer TDelete;
156
+ } ? {
157
+ delete: (input: TDelete) => Promise<string>;
158
+ } : Record<never, never>);
76
159
  /** Per-source runtime accessor: `client.sources.category.fetch()` → `Promise<CategoryFragment[]>` etc. */
77
160
  type SourceAccessors<TSources extends SourceMap> = { [K in keyof TSources]: {
78
161
  fetch: SourceFetcherFor<TSources[K]>;
79
- } };
162
+ } & SourceWritersFor<TSources[K]>; };
80
163
  //#endregion
81
164
  //#region src/upload.d.ts
82
165
  type UploadMetadata = {
@@ -163,5 +246,39 @@ type LocalDate = {
163
246
  //#region src/source-subscription.d.ts
164
247
  declare function createSourceSubscription(source: SourceDef): string;
165
248
  //#endregion
166
- export { type ConnectionContext, type ConnectionEvents, ContelloClient, type ContelloClientOptions, type DownloadResult, type HttpAgentOptions, type LocalDate, type LocalDateTime, type OperationDef, type OperationKind, type OperationMap, type ProxyResult, type Rpc, type Schema, type SourceAccessors, type SourceCardinality, type SourceDef, type SourceMap, type UploadData, type UploadMetadata, type UploadOptions, createContelloClient, createSourceSubscription };
249
+ //#region src/source-mutation.d.ts
250
+ type SourceMutationKind = keyof SourceMutations;
251
+ /** What a write takes: the caller's input, and the id for a delete. */
252
+ type SourceMutationValues = {
253
+ input?: unknown;
254
+ id?: string | undefined;
255
+ };
256
+ /** The mutation document for one write operation of a source. Cached per source + kind. */
257
+ declare function createSourceMutation(source: SourceDef, kind: SourceMutationKind): string;
258
+ /**
259
+ * Variables for a write: one per bound argument, the input nested under the envelope field when
260
+ * the argument is a one-field wrapper, and everything encoded on the way out (LocalDate /
261
+ * LocalDateTime structs become wire strings) — every caller goes through here, so no write path
262
+ * can skip the encoding.
263
+ */
264
+ declare function createSourceMutationVariables(binding: SourceMutationBinding, values: SourceMutationValues): Record<string, unknown>;
265
+ /** Pulls the id out of whatever shape the mutation answered with. */
266
+ declare function readSourceMutationId(binding: SourceMutationBinding, result: unknown): string;
267
+ //#endregion
268
+ //#region src/built-in-mutations.d.ts
269
+ /**
270
+ * Write bindings for the built-in sources. Unlike entity mutations these are not derived — their
271
+ * field and argument names are fixed parts of every Contello schema — but the generator still
272
+ * checks each one against the introspected schema before emitting it, so a server without them
273
+ * (or with a different shape) simply yields a read-only source.
274
+ *
275
+ * Routes are the odd ones: `updateContelloRoute` is keyed by the `path` inside its input rather
276
+ * than by an id, and it replaces the route rather than patching it. `deleteContelloRoute` takes a
277
+ * bare `id` and answers with a bare id string.
278
+ *
279
+ * Assets have no `create` — an asset comes into being through `client.upload()`, not a mutation.
280
+ */
281
+ declare const BUILT_IN_MUTATIONS: Partial<Record<SourceCardinality, SourceMutations>>;
282
+ //#endregion
283
+ export { BUILT_IN_MUTATIONS, type ConnectionContext, type ConnectionEvents, ContelloClient, type ContelloClientOptions, type DownloadResult, type HttpAgentOptions, type LocalDate, type LocalDateTime, type OperationDef, type OperationKind, type OperationMap, type ProxyResult, type Rpc, type Schema, type SourceAccessors, type SourceCardinality, type SourceDef, type SourceMap, type SourceMutationArgument, type SourceMutationBinding, type SourceMutationKind, type SourceMutationResult, type SourceMutationValues, type SourceMutations, type SourceWrites, type UploadData, type UploadMetadata, type UploadOptions, createContelloClient, createSourceMutation, createSourceMutationVariables, createSourceSubscription, readSourceMutationId };
167
284
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/http.ts","../src/types.ts","../src/upload.ts","../src/client.ts","../src/scalars.ts","../src/source-subscription.ts"],"mappings":";;;;KAiCY,gBAAA;EACV,UAAA;EACA,OAAA;EACA,oBAAA;EACA,gBAAA;AAAA;AAAA,KAcU,cAAA;EACV,QAAA;EACA,IAAA;EACA,KAAA,IAAS,OAAA,CAAQ,UAAA;EACjB,MAAA,IAAU,cAAA,CAAe,UAAA;AAAA;AAAA,KAiCf,WAAA;EACV,MAAA;EACA,OAAA,EAAS,OAAA;EACT,MAAA,IAAU,cAAA,CAAe,UAAA;AAAA;;;KC3Ff,aAAA;AAAA,KAEA,YAAA,iCAEG,MAAA,iCACC,aAAA,GAAgB,aAAA;EAE9B,QAAA;EACA,IAAA,EAAM,KAAA,EDyBI;ECvBV,QAAA,GAAW,OAAA;EAEX,WAAA,GAAc,UAAA;AAAA;AAAA,KAGJ,YAAA,GAAe,MAAM,SAAS,YAAA;AAAA,KAE9B,WAAA,WAAsB,YAAA,IAAgB,WAAA,CAAY,CAAA;AAAA,KAClD,cAAA,WAAyB,YAAA,IAAgB,WAAA,CAAY,CAAA;AAAA,KAErD,SAAA,WAAoB,YAAA,IAAgB,CAAA,kCAC5C,cAAA,CAAe,CAAA,UAAW,MAAA,wBAClB,aAAA,CAAc,WAAA,CAAY,CAAA,MAC/B,SAAA,EAAW,cAAA,CAAe,CAAA,MAAO,aAAA,CAAc,WAAA,CAAY,CAAA,KAC9D,cAAA,CAAe,CAAA,UAAW,MAAA,wBAClB,OAAA,CAAQ,WAAA,CAAY,CAAA,MACzB,SAAA,EAAW,cAAA,CAAe,CAAA,MAAO,OAAA,CAAQ,WAAA,CAAY,CAAA;AAAA,KAEhD,GAAA,WAAc,YAAA,kBACZ,CAAA,GAAI,SAAA,CAAU,CAAA,CAAE,CAAA;;;;;;;;;KA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,0BACnD,OAAA,EAAS,qBAAA,CAAsB,OAAA,IAC9B,cAAA,CAAe,OAAA;;;KChSN,aAAA;EACV,IAAA;EACA,KAAA;EACA,GAAA;EACA,IAAA;EACA,MAAA;EACA,MAAA;AAAA;AAAA,KAGU,SAAA;EACV,IAAA;EACA,KAAA;EACA,GAAA;AAAA;;;iBCcc,wBAAA,CAAyB,MAAiB,EAAT,SAAS"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/http.ts","../src/types.ts","../src/upload.ts","../src/client.ts","../src/scalars.ts","../src/source-subscription.ts","../src/source-mutation.ts","../src/built-in-mutations.ts"],"mappings":";;;KAiCY;EACV;EACA;EACA;EACA;;KAcU;EACV;EACA;EACA,SAAS,QAAQ;EACjB,UAAU,eAAe;;KAiCf;EACV;EACA,SAAS;EACT,UAAU,eAAe;;;;KC3Ff;KAEA,aACV,mBACA,aAAa,yBACb,cAAc,gBAAgB;EAE9B;EACA,MAAM;;EAEN,WAAW;;EAEX,cAAc;;KAGJ,eAAe,eAAe;KAE9B,YAAY,UAAU,gBAAgB,YAAY;KAClD,eAAe,UAAU,gBAAgB,YAAY;KAErD,UAAU,UAAU,gBAAgB,mCAC5C,eAAe,WAAW,8BAClB,cAAc,YAAY,OAC/B,WAAW,eAAe,OAAO,cAAc,YAAY,MAC9D,eAAe,WAAW,8BAClB,QAAQ,YAAY,OACzB,WAAW,eAAe,OAAO,QAAQ,YAAY;KAEhD,IAAI,UAAU,mBACvB,WAAW,IAAI,UAAU,EAAE;;;;;;;;;KAWlB;;;;;;KAOA;;EAEV;;EAEA;;EAEA;;;;;EAKA;;;;;;;;KASU;;;;;KAMA;;EAEV;EACA,WAAW;EACX,QAAQ;;;KAIE;EACV,SAAS;EACT,SAAS;EACT,SAAS;;;;;;;KAQC;EACV;EACA;EACA;;KAGU,UACV,gCACA,qBAAqB,oBAAoB,mBACzC,mBACA;;EAGA;;EAEA;;EAEA;;EAEA,YAAY;EACZ,SAAS;EACT,eAAe;;EAEf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AAO,gBAAgB,aAAa,kBAAkB,IAAI;MAQ1D,WAAW,gBAAgB,kBAAkB,iBAAiB,gBAAgB;EAQ5E,QAAQ;EAIR,WAAW;EAKX,QAAQ;;;;;;EAUd,UAAU,OAAO,eAAe,YAAY,sCAAsC,cAAc;EAuFhG,SAAS,iBAAiB,QAAQ;EAIlC,SAAS,cAAc,SAAS,0BAA0B,QAAQ;EAIlE,OAAO,MAAM,YAAY,MAAM,gBAAgB,UAAU,4BAA4B;EAMrF,QAAQ,OAAO,eAAe,YAAY,sCAAsC,QAAQ;;iBAS1E,qBAAqB,gBAAgB,gCACnD,SAAS,sBAAsB,WAC9B,eAAe;;;KChSN;EACV;EACA;EACA;EACA;EACA;EACA;;KAGU;EACV;EACA;EACA;;;;iBCcc,yBAAyB,QAAQ;;;KCvBrC,2BAA2B;;KAG3B;EACV;EACA;;;iBAwBc,qBAAqB,QAAQ,WAAW,MAAM;;;;;;;iBA+B9C,8BACd,SAAS,uBACT,QAAQ,uBACP;;iBAYa,qBAAqB,SAAS,uBAAuB;;;;;;;;;;;;;;;cChExD,oBAAoB,QAAQ,OAAO,mBAAmB"}
package/dist/index.js CHANGED
@@ -17,7 +17,7 @@ const FORWARDED_PROXY_HEADERS = /* @__PURE__ */ new Set([
17
17
  "age"
18
18
  ]);
19
19
  function assertSafeSegment(segment, kind) {
20
- if (!SAFE_SEGMENT.test(segment) || segment === "." || segment === "..") throw new Error(`invalid ${kind}`);
20
+ if (segment === "." || segment === ".." || !SAFE_SEGMENT.test(segment)) throw new Error(`invalid ${kind}`);
21
21
  }
22
22
  function assertSafeAssetPath(path, kind) {
23
23
  if (path === "") throw new Error(`invalid ${kind}`);
@@ -302,6 +302,55 @@ function buildRpc(operations, subscribe) {
302
302
  return rpc;
303
303
  }
304
304
  //#endregion
305
+ //#region src/source-mutation.ts
306
+ const cache$1 = /* @__PURE__ */ new WeakMap();
307
+ /**
308
+ * A create/update answers with the entity itself, so it selects the source's own fragment — the
309
+ * write comes back in the same shape a read does and needs no follow-up fetch. A delete answers
310
+ * with an id, either wrapped in a response object or, for routes, as a bare scalar.
311
+ */
312
+ function createSelection$1(source, binding) {
313
+ const declarations = binding.arguments.map((argument) => `$${argument.name}: ${argument.type}`).join(", ");
314
+ const args = binding.arguments.map((argument) => `${argument.name}: $${argument.name}`).join(", ");
315
+ const selection = {
316
+ entity: ` { ...${source.fragment} }`,
317
+ idObject: " { id }",
318
+ idScalar: ""
319
+ }[binding.result];
320
+ const operation = `mutation(${declarations}) { result: ${binding.field}(${args})${selection} }`;
321
+ return binding.result === "entity" ? `${source.document}\n${operation}` : operation;
322
+ }
323
+ function getSourceMutation(source, kind) {
324
+ return source.mutations?.[kind];
325
+ }
326
+ /** The mutation document for one write operation of a source. Cached per source + kind. */
327
+ function createSourceMutation(source, kind) {
328
+ const binding = getSourceMutation(source, kind);
329
+ if (!binding) throw new Error(`@contello/client: source "${source.__model}" has no ${kind} mutation — the schema exposes none for this model`);
330
+ const cached = cache$1.get(source);
331
+ if (cached?.[kind] !== void 0) return cached[kind];
332
+ const doc = createSelection$1(source, binding);
333
+ const entry = cached ?? {};
334
+ entry[kind] = doc;
335
+ cache$1.set(source, entry);
336
+ return doc;
337
+ }
338
+ /**
339
+ * Variables for a write: one per bound argument, the input nested under the envelope field when
340
+ * the argument is a one-field wrapper, and everything encoded on the way out (LocalDate /
341
+ * LocalDateTime structs become wire strings) — every caller goes through here, so no write path
342
+ * can skip the encoding.
343
+ */
344
+ function createSourceMutationVariables(binding, values) {
345
+ const variables = {};
346
+ for (const argument of binding.arguments) variables[argument.name] = argument.from === "id" ? values.id : argument.envelope ? { [argument.envelope]: values.input } : values.input;
347
+ return transformVariables(variables);
348
+ }
349
+ /** Pulls the id out of whatever shape the mutation answered with. */
350
+ function readSourceMutationId(binding, result) {
351
+ return binding.result === "idScalar" ? result : result.id;
352
+ }
353
+ //#endregion
305
354
  //#region src/source-subscription.ts
306
355
  const cache = /* @__PURE__ */ new WeakMap();
307
356
  function createSelection(source) {
@@ -323,12 +372,45 @@ function createSourceSubscription(source) {
323
372
  }
324
373
  //#endregion
325
374
  //#region src/sources.ts
375
+ const WRITE_KINDS = [
376
+ "create",
377
+ "update",
378
+ "delete"
379
+ ];
326
380
  function createSources(sources, subscribe) {
327
381
  const out = {};
328
382
  for (const [name, source] of Object.entries(sources)) {
329
383
  const def = source;
330
384
  const accessor = createSourceAccessor(name, def, createSourceSubscription(def), subscribe);
331
- if (accessor) out[name] = accessor;
385
+ if (accessor) out[name] = {
386
+ ...accessor,
387
+ ...createSourceWriters(name, def, subscribe)
388
+ };
389
+ }
390
+ return out;
391
+ }
392
+ /**
393
+ * Write accessors for the mutations the generator bound to this source. A create/update answers
394
+ * with the entity in the source's fragment shape, a delete with the id it removed. Nothing is
395
+ * cached here — `@contello/store` collections take the same payload into their cache.
396
+ */
397
+ function createSourceWriters(name, def, subscribe) {
398
+ const out = {};
399
+ for (const kind of WRITE_KINDS) {
400
+ const binding = def.mutations?.[kind];
401
+ if (!binding) continue;
402
+ const doc = createSourceMutation(def, kind);
403
+ out[kind] = (input) => {
404
+ const variables = createSourceMutationVariables(binding, kind === "delete" ? {
405
+ input,
406
+ id: input.id
407
+ } : { input });
408
+ return wrap(`source:${name}:${kind}`, async () => {
409
+ const response = await firstAsync(subscribe(doc, variables));
410
+ if (response?.result === void 0 || response.result === null) throw new Error(`@contello/client: ${kind} on source "${name}" returned no entity`);
411
+ return kind === "delete" ? readSourceMutationId(binding, response.result) : response.result;
412
+ }, graphqlOperationAttributes(doc, variables));
413
+ };
332
414
  }
333
415
  return out;
334
416
  }
@@ -443,7 +525,7 @@ function resolveRefs(refs, flatMap) {
443
525
  }
444
526
  //#endregion
445
527
  //#region src/upload.ts
446
- const DEFAULT_CHUNK_SIZE = 4 * 1024 * 1024;
528
+ const DEFAULT_CHUNK_SIZE = 4194304;
447
529
  function isUint8Array(data) {
448
530
  return data instanceof Uint8Array;
449
531
  }
@@ -472,7 +554,7 @@ function upload(url, project, token, data, meta, options) {
472
554
  }
473
555
  ws.send(JSON.stringify({ type: "done" }));
474
556
  }
475
- const MAX_BUFFER_SIZE = 8 * 1024 * 1024;
557
+ const MAX_BUFFER_SIZE = 8388608;
476
558
  function waitForBufferedDrain() {
477
559
  return new Promise((resolveDrain, rejectDrain) => {
478
560
  const check = () => {
@@ -730,6 +812,71 @@ function createContelloClient(options) {
730
812
  return new ContelloClient(options);
731
813
  }
732
814
  //#endregion
733
- export { ContelloClient, createContelloClient, createSourceSubscription };
815
+ //#region src/built-in-mutations.ts
816
+ /**
817
+ * Write bindings for the built-in sources. Unlike entity mutations these are not derived — their
818
+ * field and argument names are fixed parts of every Contello schema — but the generator still
819
+ * checks each one against the introspected schema before emitting it, so a server without them
820
+ * (or with a different shape) simply yields a read-only source.
821
+ *
822
+ * Routes are the odd ones: `updateContelloRoute` is keyed by the `path` inside its input rather
823
+ * than by an id, and it replaces the route rather than patching it. `deleteContelloRoute` takes a
824
+ * bare `id` and answers with a bare id string.
825
+ *
826
+ * Assets have no `create` — an asset comes into being through `client.upload()`, not a mutation.
827
+ */
828
+ const BUILT_IN_MUTATIONS = {
829
+ route: {
830
+ create: {
831
+ field: "createContelloRoute",
832
+ arguments: [{
833
+ name: "route",
834
+ type: "ContelloRouteInput!",
835
+ from: "input"
836
+ }],
837
+ result: "entity"
838
+ },
839
+ update: {
840
+ field: "updateContelloRoute",
841
+ arguments: [{
842
+ name: "route",
843
+ type: "ContelloRouteInput!",
844
+ from: "input"
845
+ }],
846
+ result: "entity"
847
+ },
848
+ delete: {
849
+ field: "deleteContelloRoute",
850
+ arguments: [{
851
+ name: "id",
852
+ type: "String",
853
+ from: "id"
854
+ }],
855
+ result: "idScalar"
856
+ }
857
+ },
858
+ asset: {
859
+ update: {
860
+ field: "updateContelloAsset",
861
+ arguments: [{
862
+ name: "request",
863
+ type: "ContelloAssetUpdateInput!",
864
+ from: "input"
865
+ }],
866
+ result: "entity"
867
+ },
868
+ delete: {
869
+ field: "deleteContelloAsset",
870
+ arguments: [{
871
+ name: "id",
872
+ type: "String!",
873
+ from: "id"
874
+ }],
875
+ result: "idObject"
876
+ }
877
+ }
878
+ };
879
+ //#endregion
880
+ export { BUILT_IN_MUTATIONS, ContelloClient, createContelloClient, createSourceMutation, createSourceMutationVariables, createSourceSubscription, readSourceMutationId };
734
881
 
735
882
  //# sourceMappingURL=index.js.map