@capacms/sdk 1.0.0-next.1 → 1.0.0-next.11

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.
Files changed (93) hide show
  1. package/CHANGELOG.md +487 -0
  2. package/README.md +1810 -183
  3. package/bin/capa-codegen.js +192 -5
  4. package/bin/capa.js +235 -0
  5. package/bin/graphql-project.js +142 -0
  6. package/bin/project-env.js +58 -0
  7. package/dist/client.d.ts +89 -4
  8. package/dist/client.js +40 -5
  9. package/dist/codegen.d.ts +55 -0
  10. package/dist/codegen.js +320 -39
  11. package/dist/config.d.ts +5 -36
  12. package/dist/config.js +47 -1
  13. package/dist/esm/image/index.d.ts +187 -0
  14. package/dist/esm/image/index.js +353 -0
  15. package/dist/esm/image/shared-params.generated.d.ts +314 -0
  16. package/dist/esm/image/shared-params.generated.js +680 -0
  17. package/dist/esm/nextjs/image-loader.d.ts +60 -0
  18. package/dist/esm/nextjs/image-loader.js +67 -0
  19. package/dist/esm/nextjs/overlay.d.ts +30 -0
  20. package/dist/esm/nextjs/overlay.js +75 -0
  21. package/dist/esm/overlay/index.d.ts +32 -0
  22. package/dist/esm/overlay/index.js +576 -0
  23. package/dist/esm/overlay/protocol.d.ts +187 -0
  24. package/dist/esm/overlay/protocol.js +240 -0
  25. package/dist/esm/package.json +4 -0
  26. package/dist/graphql-codegen.d.ts +117 -0
  27. package/dist/graphql-codegen.js +705 -0
  28. package/dist/http.js +1 -1
  29. package/dist/image/index.d.ts +187 -0
  30. package/dist/image/index.js +362 -0
  31. package/dist/image/shared-params.generated.d.ts +314 -0
  32. package/dist/image/shared-params.generated.js +700 -0
  33. package/dist/index.d.ts +3 -3
  34. package/dist/index.js +2 -1
  35. package/dist/next/attrs.d.ts +84 -10
  36. package/dist/next/attrs.js +119 -2
  37. package/dist/next/client.d.ts +176 -32
  38. package/dist/next/client.js +212 -90
  39. package/dist/next/entry-fields.d.ts +203 -0
  40. package/dist/next/entry-fields.js +2 -0
  41. package/dist/next/errors.d.ts +136 -0
  42. package/dist/next/errors.js +214 -0
  43. package/dist/next/field-names.d.ts +37 -0
  44. package/dist/next/field-names.js +145 -0
  45. package/dist/next/graphql/build.d.ts +27 -0
  46. package/dist/next/graphql/build.js +98 -0
  47. package/dist/next/graphql/documents.d.ts +67 -0
  48. package/dist/next/graphql/documents.js +35 -0
  49. package/dist/next/graphql/edit-mode.d.ts +16 -0
  50. package/dist/next/graphql/edit-mode.js +93 -0
  51. package/dist/next/graphql/filter-values.d.ts +34 -0
  52. package/dist/next/graphql/filter-values.js +96 -0
  53. package/dist/next/graphql/introspection.d.ts +89 -0
  54. package/dist/next/graphql/introspection.js +102 -0
  55. package/dist/next/graphql/plan.d.ts +115 -0
  56. package/dist/next/graphql/plan.js +531 -0
  57. package/dist/next/graphql/request.d.ts +228 -0
  58. package/dist/next/graphql/request.js +283 -0
  59. package/dist/next/graphql/rest.d.ts +66 -0
  60. package/dist/next/graphql/rest.js +502 -0
  61. package/dist/next/graphql/selection.d.ts +55 -0
  62. package/dist/next/graphql/selection.js +212 -0
  63. package/dist/next/graphql/sha256.d.ts +13 -0
  64. package/dist/next/graphql/sha256.js +86 -0
  65. package/dist/next/graphql/summary.d.ts +83 -0
  66. package/dist/next/graphql/summary.js +151 -0
  67. package/dist/next/graphql/tree-layout.d.ts +36 -0
  68. package/dist/next/graphql/tree-layout.js +20 -0
  69. package/dist/next/graphql/tree.d.ts +171 -0
  70. package/dist/next/graphql/tree.js +258 -0
  71. package/dist/next/graphql/typed.d.ts +261 -0
  72. package/dist/next/graphql/typed.js +146 -0
  73. package/dist/next/index.d.ts +30 -5
  74. package/dist/next/index.js +32 -1
  75. package/dist/next/inflate.d.ts +51 -0
  76. package/dist/next/inflate.js +243 -0
  77. package/dist/next/key-family.d.ts +31 -0
  78. package/dist/next/key-family.js +66 -0
  79. package/dist/next/select-types.d.ts +58 -5
  80. package/dist/next/system-keys.d.ts +27 -0
  81. package/dist/next/system-keys.js +42 -0
  82. package/dist/nextjs/image-loader.d.ts +60 -0
  83. package/dist/nextjs/image-loader.js +71 -0
  84. package/dist/nextjs/index.d.ts +484 -5
  85. package/dist/nextjs/index.js +688 -6
  86. package/dist/nextjs/overlay.d.ts +30 -0
  87. package/dist/nextjs/overlay.js +78 -0
  88. package/dist/overlay/index.d.ts +14 -2
  89. package/dist/overlay/index.js +282 -43
  90. package/dist/overlay/protocol.d.ts +98 -2
  91. package/dist/overlay/protocol.js +151 -4
  92. package/dist/webhooks.d.ts +8 -0
  93. package/package.json +63 -15
@@ -0,0 +1,314 @@
1
+ /**
2
+ * Query parsing, validation and canonicalisation for `GET /files/*`
3
+ * (ADMIN_UI_OVERHAUL §0i.1, "The image processor, made tight").
4
+ *
5
+ * PURE ON PURPOSE. Nothing here touches sharp, the network or the database, so
6
+ * the whole parameter surface — every accepted value, every rejection, and the
7
+ * canonical spelling of every URL — is testable without a server and without a
8
+ * live Backblaze object. That matters for this route in particular: its eight
9
+ * parity fixtures are all 400/404 cases because a DB hit immediately leaves the
10
+ * process (docs/TURBINE_PORT_NOTES.md, "Deferred to the P6 cloud parity pass"),
11
+ * so the transform surface has no golden coverage at all unless it is reachable
12
+ * from a unit test.
13
+ *
14
+ * THE RULE THAT SHAPES EVERYTHING BELOW: a caller that sends only today's three
15
+ * parameters (`width`, `height`, `blur`) must get today's response. Every
16
+ * default here is therefore today's behaviour, and the one place that could
17
+ * have broken it — the canonical-query redirect — is deliberately gated on a
18
+ * NEW parameter being present. See `canonicalise`.
19
+ *
20
+ * WHY IT LIVES IN @capa/shared. `@capacms/sdk` builds image URLs, and a URL
21
+ * that is not spelled the way `canonicalise` spells it costs every first view
22
+ * a 301 that the edge then keeps for a year. So the SDK compiles this file into
23
+ * its own build rather than keeping a second copy of the rules, and its tests
24
+ * run what it builds back through `canonicalise`. The API imports it from here.
25
+ */
26
+ export type ImageFit = "inside" | "cover" | "contain";
27
+ export type ImageFormat = "webp" | "avif" | "jpeg" | "png" | "auto";
28
+ export type ConcreteFormat = Exclude<ImageFormat, "auto">;
29
+ /** Content type emitted for each explicitly requestable output format. */
30
+ export declare const OUTPUT_MIME: Record<ConcreteFormat, string>;
31
+ /**
32
+ * The longest edge this route will ever produce.
33
+ *
34
+ * A cap rather than a clamp, because a clamp makes two different URLs return
35
+ * the same bytes and therefore occupy two edge objects for one representation.
36
+ * 4096 is the §0i.1 figure; it is also comfortably above any real layout width
37
+ * at dpr 3 (1365 CSS px).
38
+ */
39
+ export declare const MAX_OUTPUT_EDGE = 4096;
40
+ /** Today's blur bounds, previously applied as a silent clamp. */
41
+ export declare const MIN_BLUR = 1;
42
+ export declare const MAX_BLUR = 256;
43
+ /** Default quality for lossy outputs except AVIF. Ignored for png, which is lossless. */
44
+ export declare const DEFAULT_QUALITY = 80;
45
+ /**
46
+ * AVIF's own default quality (Penelope Hospitality onboarding, #300). At 80,
47
+ * AVIF came back larger than WebP at 80; 60 at effort 2 is 16 to 18% smaller
48
+ * than WebP with equal or better SSIM on eight client photos. The route's
49
+ * encoder and the effort live in apps/api (image-transform.ts); the number
50
+ * lives here because canonicalQuery has to know it (see droppableQuality).
51
+ */
52
+ export declare const AVIF_DEFAULT_QUALITY = 60;
53
+ /**
54
+ * The quality an encode into `output` uses when the URL names none.
55
+ *
56
+ * The route only ever calls this with a format the URL asked for, explicitly or
57
+ * through `auto`: with no `format` and no `quality` it selects no encoder at
58
+ * all and the source keeps its own format (`resolveOutputFormat`).
59
+ */
60
+ export declare function defaultQuality(output: ConcreteFormat): number;
61
+ /**
62
+ * The `quality` canonicalQuery drops from a URL with this `format`, or null
63
+ * when it keeps every value.
64
+ *
65
+ * CANONICALISATION MUST NEVER CHANGE WHAT A URL RENDERS. A value may be dropped
66
+ * only when the URL without it encodes at that same value. After #300 gave
67
+ * AVIF its own default, dropping 80 everywhere sent `format=avif&quality=80`
68
+ * to `format=avif`, which is quality 60.
69
+ *
70
+ * - `avif`: null. 80 is not AVIF's default, so it must stay. 60 is, and could
71
+ * be dropped without changing the bytes; it is KEPT ON PURPOSE. The SDK's
72
+ * frozen URLs (#316, and @capacms/sdk's own tests) include
73
+ * `format=avif&quality=60`, which would start taking a 301; and an explicit
74
+ * number keeps meaning that number if AVIF's default is retuned, which
75
+ * `format=avif` does not. The cost is two edge objects for one image when a
76
+ * caller spells the default out.
77
+ * - `auto`: null. Negotiation picks AVIF (default 60) or WebP or the source's
78
+ * own format (default 80), so no single value means "no quality" for every
79
+ * browser.
80
+ * - `webp`, `jpeg`, `png` and no format: DEFAULT_QUALITY, as before.
81
+ *
82
+ * KNOWN GAP, older than #300 and left as it is because a URL with no `format`
83
+ * keeps its spelling: with no format the output follows the SOURCE, which a URL
84
+ * cannot see. `?quality=80&width=200` on an AVIF or HEIF source is an AVIF at
85
+ * 80, and `?width=200` keeps sharp's own AVIF default (50). `?quality=80` with
86
+ * nothing else redirects to the bare URL, which is the stored file, not a
87
+ * re-encode.
88
+ */
89
+ export declare function droppableQuality(format: ImageFormat | undefined): number | null;
90
+ /**
91
+ * The parameters that existed before §0i.1.
92
+ *
93
+ * A URL built only from these is GRANDFATHERED: it is treated as already
94
+ * canonical and is never redirected, however it is spelled. Without this, every
95
+ * live `?width=100&height=50` in every customer's markup would start taking a
96
+ * 301 — which is not a behaviour anyone asked to change, and "keep today's
97
+ * calls byte-identical" outranks de-duplicating an edge object that has been
98
+ * duplicated for years already.
99
+ */
100
+ export declare const LEGACY_PARAMS: Set<string>;
101
+ /** Parameters introduced by §0i.1. Their presence is what enables canonicalisation. */
102
+ export declare const NEW_PARAMS: Set<string>;
103
+ export interface ImageParams {
104
+ /** Requested width AFTER dpr multiplication. */
105
+ width?: number;
106
+ /** Requested height AFTER dpr multiplication. */
107
+ height?: number;
108
+ blur?: number;
109
+ format?: ImageFormat;
110
+ /** Present only when the caller asked for it — see `quality` in `buildSignature`. */
111
+ quality?: number;
112
+ fit: ImageFit;
113
+ dpr: number;
114
+ upscale: boolean;
115
+ keepMetadata: boolean;
116
+ /**
117
+ * `fp` and `rect` (ENG-5959), read by `parseFocalParams` and only for a
118
+ * project with the focal point and crop on; absent otherwise, and then the
119
+ * two are unknown keys like any other.
120
+ */
121
+ focalPoint?: FocalPoint;
122
+ rect?: CropRect;
123
+ /** True when any parameter that changes the bytes was supplied. */
124
+ wantsTransform: boolean;
125
+ }
126
+ export interface ParamRejection {
127
+ error: string;
128
+ param: string;
129
+ }
130
+ export type ParseResult = {
131
+ ok: true;
132
+ params: ImageParams;
133
+ } | {
134
+ ok: false;
135
+ rejection: ParamRejection;
136
+ };
137
+ /**
138
+ * Parse and validate the whole query in one pass.
139
+ *
140
+ * Unknown VALUES for a known parameter are rejected. Unknown KEYS are not:
141
+ * `?v=3` cache-busters and analytics tags are real traffic on a public CDN
142
+ * origin, and turning them into 400s would break callers who are not using the
143
+ * image surface at all. They are carried through canonicalisation untouched.
144
+ */
145
+ export declare function parseImageParams(query: Record<string, unknown>): ParseResult;
146
+ export interface CanonicalResult {
147
+ /** The canonical query STRING, without a leading `?`. Empty when there is none. */
148
+ query: string;
149
+ /** True when the caller's spelling already matches and no redirect is owed. */
150
+ isCanonical: boolean;
151
+ }
152
+ /**
153
+ * The canonical spelling of a query, so `?width=200&format=webp` and
154
+ * `?format=webp&width=200` are one edge object rather than two.
155
+ *
156
+ * GATED, DELIBERATELY. Canonicalisation only applies once the caller uses a
157
+ * parameter introduced by §0i.1. A URL made only of `width`/`height`/`blur` is
158
+ * declared canonical as received, whatever its key order — see LEGACY_PARAMS
159
+ * for why. The gate is also what keeps the `files-404-params-ignored` parity
160
+ * fixture (`?width=100&height=50&blur=9`) from becoming a 301.
161
+ *
162
+ * @param rawQuery the query string as received, without the leading `?`
163
+ */
164
+ export declare function canonicalise(rawQuery: string, params: ImageParams, options?: CanonicalOptions): CanonicalResult;
165
+ export interface CanonicalOptions {
166
+ /**
167
+ * True when `fp` and `rect` are parameters here (the project has the focal
168
+ * point and crop on): they are then spelled from `params` like the others,
169
+ * and their presence enables canonicalisation. False or absent, they are
170
+ * unknown keys, kept verbatim, exactly as before ENG-5959.
171
+ */
172
+ focal?: boolean;
173
+ }
174
+ /**
175
+ * The canonical spelling itself, without `canonicalise`'s gate.
176
+ *
177
+ * `canonicalise` is this plus the LEGACY_PARAMS grandfather clause. For a query
178
+ * that uses a new parameter the two agree; for one made only of
179
+ * `width`/`height`/`blur` this still sorts and re-spells it, which the API
180
+ * accepts as canonical too, since the gate declares any legacy spelling
181
+ * canonical. So a URL built with this is never redirected, whichever
182
+ * parameters it carries. @capacms/sdk builds every image URL with it.
183
+ *
184
+ * @param rawQuery the query string, without the leading `?`; read only for the
185
+ * keys this module does not know, which are kept
186
+ */
187
+ export declare function canonicalQuery(rawQuery: string, params: ImageParams, options?: CanonicalOptions): string;
188
+ /**
189
+ * A stable string identifying the transform, for the ETag.
190
+ *
191
+ * Built from the RESOLVED parameters rather than the URL, so two spellings of
192
+ * one representation share an ETag even when the redirect is not in play.
193
+ * `auto` resolves per request, so the caller passes the resolved format in.
194
+ */
195
+ export declare function buildSignature(params: ImageParams, resolvedFormat?: ConcreteFormat | null): string;
196
+ /**
197
+ * Which concrete format `format=auto` resolves to for this request.
198
+ *
199
+ * Returns null when the client advertises neither modern format, in which case
200
+ * the source format is kept — `auto` never makes a response worse than the
201
+ * object already is.
202
+ *
203
+ * `q=0` is an explicit REFUSAL in RFC 9110 content negotiation, not a weak
204
+ * preference, so an entry carrying it is skipped rather than matched.
205
+ */
206
+ export declare function resolveAutoFormat(accept: string | undefined | null): "avif" | "webp" | null;
207
+ /** A focal point: fractions of the oriented image, from the left and the top. */
208
+ export interface FocalPoint {
209
+ x: number;
210
+ y: number;
211
+ }
212
+ /** A crop: fractions of the oriented image. */
213
+ export interface CropRect {
214
+ x: number;
215
+ y: number;
216
+ width: number;
217
+ height: number;
218
+ }
219
+ /** `fp` and `rect`: parameters only where the focal point and crop are on. */
220
+ export declare const FOCAL_PARAMS: Set<string>;
221
+ /** Decimal places a fraction keeps: a ten-thousandth, under a pixel up to 10,000 px. */
222
+ export declare const FRACTION_DECIMALS = 4;
223
+ /** A number rounded to FRACTION_DECIMALS. */
224
+ export declare function roundFraction(n: number): number;
225
+ /**
226
+ * A focal point from stored metadata or a request body, or null when it is
227
+ * not one: an object whose `x` and `y` are numbers from 0 to 1.
228
+ */
229
+ export declare function readFocalPoint(value: unknown): FocalPoint | null;
230
+ /**
231
+ * A crop from stored metadata or a request body, or null when it is not one:
232
+ * `x`, `y`, `width` and `height` from 0 to 1, a width and a height above 0,
233
+ * and inside the image.
234
+ */
235
+ export declare function readCrop(value: unknown): CropRect | null;
236
+ /**
237
+ * A focal point exactly as the api stores one (`normaliseFocalPatch` in
238
+ * apps/api routes/v2/file-uploads.ts): `{ x, y }` and no other key, numbers
239
+ * from 0 to 1. Stricter than `readFocalPoint`, which reads one out of any
240
+ * object that has them: this is the test for "ENG-5959 wrote this", which a
241
+ * replace carries over and the legacy routes hide. Anything else under the
242
+ * name is a project's own metadata and is left alone.
243
+ */
244
+ export declare function isStoredFocalPoint(value: unknown): value is FocalPoint;
245
+ /** A crop exactly as the api stores one: `{ x, y, width, height }` and no other key. See `isStoredFocalPoint`. */
246
+ export declare function isStoredCrop(value: unknown): value is CropRect;
247
+ export type FocalParseResult = {
248
+ ok: true;
249
+ focalPoint?: FocalPoint;
250
+ rect?: CropRect;
251
+ } | {
252
+ ok: false;
253
+ rejection: ParamRejection;
254
+ };
255
+ /**
256
+ * `fp` and `rect`, for a project with the focal point and crop on. The route
257
+ * calls it after it knows the file's project; anywhere else the two are
258
+ * unknown keys and this is never called.
259
+ */
260
+ export declare function parseFocalParams(query: Record<string, unknown>): FocalParseResult;
261
+ /** What the file says, from its `file_uploads.metadata`. */
262
+ export interface StoredFocal {
263
+ focalPoint: FocalPoint | null;
264
+ crop: CropRect | null;
265
+ }
266
+ /**
267
+ * The crop and focal point a request applies: the URL's, else the file's,
268
+ * and the file's only for a `fit=cover` crop to both a width and a height.
269
+ * A blur placeholder keeps its own square geometry and applies only a `rect`
270
+ * the URL names.
271
+ */
272
+ /**
273
+ * A crop to a shape: `fit=cover` with a width and a height, and no blur. The
274
+ * one request the file's stored focal point and crop apply to.
275
+ */
276
+ export declare function isCoverCrop(params: ImageParams): boolean;
277
+ /**
278
+ * True when this request's bytes depend on the file's STORED focal point or
279
+ * crop: a cover crop that does not name both `fp` and `rect` itself. Its
280
+ * bytes change under the same URL when an editor saves either, so a browser
281
+ * must not keep them as long as an immutable variant (`GET /files/*`).
282
+ */
283
+ export declare function followsStoredFocal(params: ImageParams): boolean;
284
+ export declare function appliedFocal(params: ImageParams, stored: StoredFocal | null): {
285
+ rect: CropRect | null;
286
+ focalPoint: FocalPoint | null;
287
+ };
288
+ /** A region of the oriented source, in whole pixels, for sharp's `extract`. */
289
+ export interface PixelRegion {
290
+ left: number;
291
+ top: number;
292
+ width: number;
293
+ height: number;
294
+ }
295
+ /**
296
+ * The part of the source to cut out before the resize, or null for none.
297
+ *
298
+ * `source` is the image as a person sees it. `target` is the `fit=cover` box
299
+ * (width and height after dpr), or null for any other request. The crop is cut
300
+ * first; with a target and a focal point, the window of the target's shape
301
+ * that a cover resize would keep is then moved, inside the crop, so its centre
302
+ * is as close to the focal point as the crop allows. Without a focal point the
303
+ * crop alone is returned and the resize centres in it as it always has.
304
+ *
305
+ * The window is the largest of the target's shape that fits the crop, so the
306
+ * resize that follows scales it to the target without cutting anything more.
307
+ */
308
+ export declare function focalRegion(source: {
309
+ width: number;
310
+ height: number;
311
+ }, target: {
312
+ width: number;
313
+ height: number;
314
+ } | null, rect: CropRect | null, focalPoint: FocalPoint | null): PixelRegion | null;