@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
package/dist/client.d.ts CHANGED
@@ -28,7 +28,68 @@ export interface ListOptions {
28
28
  */
29
29
  where?: Record<string, string | number | boolean>;
30
30
  }
31
- export interface Page<T> {
31
+ /**
32
+ * One field of a `/v2/api` entry as Capa stores it: `{ type, value, sortOrder }`.
33
+ * A relation field's `value` is the related entry's id, or a list of ids.
34
+ */
35
+ export interface FieldValue<V = unknown> {
36
+ type?: string;
37
+ value: V;
38
+ sortOrder?: number;
39
+ [key: string]: unknown;
40
+ }
41
+ /**
42
+ * One entry of `relations`: a related entry as `/v2/api` sends it.
43
+ *
44
+ * Unlike a row of `data`, it keeps its fields under `data` only, so `id` is
45
+ * always the entry's UUID and `title` the served version's title, whatever the
46
+ * model's fields are called. `modelType` is its model's namespace. The API
47
+ * sends more columns than these (`tenantId`, `tags`, `dataModel`, its
48
+ * versions), and they come through as sent.
49
+ */
50
+ export interface RelatedEntry<D = Record<string, FieldValue>> {
51
+ id: string;
52
+ modelId: string;
53
+ /** The related entry's model namespace. */
54
+ modelType: string;
55
+ title: string | null;
56
+ data: D;
57
+ createdAt: string;
58
+ updatedAt: string;
59
+ [key: string]: unknown;
60
+ }
61
+ /**
62
+ * A read's related entries by entry id, at every level down to `depth`, each
63
+ * once however many rows name it. `{}` at depth 0, the default. An id a row
64
+ * holds with no entry here is one the key cannot read: unpublished for a
65
+ * production key, or deleted.
66
+ */
67
+ export type Relations<R = RelatedEntry> = Record<string, R>;
68
+ /**
69
+ * `getContentById` and `findOne` with `withRelations: true`: the row they
70
+ * return without it, and what the same answer carried beside it.
71
+ */
72
+ export interface OneWithRelations<T, R = RelatedEntry> {
73
+ data: T;
74
+ /** The row's instance id, read as `Page.ids` reads it (`instanceIdOf`). */
75
+ id: string | null;
76
+ relations: Relations<R>;
77
+ meta: Page<T>["meta"];
78
+ cacheTags: string[];
79
+ }
80
+ /**
81
+ * What `getContentById` and `findOne` take. A literal `withRelations: true`
82
+ * types the result as `OneWithRelations`, a literal `false` or none as the row,
83
+ * and a `boolean` (or a value of this type) as either.
84
+ */
85
+ export interface ReadOneOptions extends Pick<ListOptions, "depth"> {
86
+ /**
87
+ * Return `{ data, id, relations, meta, cacheTags }` in place of the row, so
88
+ * its related entries can be read. Sent nowhere: the request is the same.
89
+ */
90
+ withRelations?: boolean;
91
+ }
92
+ export interface Page<T, R = RelatedEntry> {
32
93
  data: T[];
33
94
  /**
34
95
  * Instance ids positionally matching `data`, `null` only when the API sent
@@ -36,6 +97,11 @@ export interface Page<T> {
36
97
  * which is the model's own field whenever one is named `id`.
37
98
  */
38
99
  ids: (string | null)[];
100
+ /**
101
+ * The related entries of `data`, by id: `page.relations[row.data.author.value]`.
102
+ * Read with `depth` of 1 or more; see `Relations`.
103
+ */
104
+ relations: Relations<R>;
39
105
  meta: {
40
106
  total?: number;
41
107
  totalPages?: number;
@@ -48,9 +114,23 @@ export interface Page<T> {
48
114
  cacheTags: string[];
49
115
  }
50
116
  export interface CapaClient {
51
- listContent<T = Record<string, unknown>>(namespace: string, options?: ListOptions): Promise<Page<T>>;
52
- getContentById<T = Record<string, unknown>>(namespace: string, id: string, options?: Pick<ListOptions, "depth">): Promise<T | null>;
53
- findOne<T = Record<string, unknown>>(namespace: string, where: Record<string, string | number | boolean>, options?: Pick<ListOptions, "depth">): Promise<T | null>;
117
+ listContent<T = Record<string, unknown>, R = RelatedEntry>(namespace: string, options?: ListOptions): Promise<Page<T, R>>;
118
+ getContentById<T = Record<string, unknown>, R = RelatedEntry>(namespace: string, id: string, options: ReadOneOptions & {
119
+ withRelations: true;
120
+ }): Promise<OneWithRelations<T, R> | null>;
121
+ getContentById<T = Record<string, unknown>>(namespace: string, id: string, options?: ReadOneOptions & {
122
+ withRelations?: false;
123
+ }): Promise<T | null>;
124
+ /** A `ReadOneOptions` value, or a `withRelations` known only at run time: either shape. */
125
+ getContentById<T = Record<string, unknown>, R = RelatedEntry>(namespace: string, id: string, options?: ReadOneOptions): Promise<T | OneWithRelations<T, R> | null>;
126
+ findOne<T = Record<string, unknown>, R = RelatedEntry>(namespace: string, where: Record<string, string | number | boolean>, options: ReadOneOptions & {
127
+ withRelations: true;
128
+ }): Promise<OneWithRelations<T, R> | null>;
129
+ findOne<T = Record<string, unknown>>(namespace: string, where: Record<string, string | number | boolean>, options?: ReadOneOptions & {
130
+ withRelations?: false;
131
+ }): Promise<T | null>;
132
+ /** A `ReadOneOptions` value, or a `withRelations` known only at run time: either shape. */
133
+ findOne<T = Record<string, unknown>, R = RelatedEntry>(namespace: string, where: Record<string, string | number | boolean>, options?: ReadOneOptions): Promise<T | OneWithRelations<T, R> | null>;
54
134
  search(q: string, options?: {
55
135
  namespace?: string;
56
136
  limit?: number;
@@ -410,4 +490,9 @@ export interface SearchHit {
410
490
  * shadowing model. `getContentById` cannot be reached for such a row.
411
491
  */
412
492
  export declare function instanceIdOf(row: unknown): string | null;
493
+ /**
494
+ * The legacy `/v2/api` client, for a legacy key (`pk_`, `sk_` or unprefixed)
495
+ * and its tenant's id. For `/api/`, GraphQL and `cap_` keys, import
496
+ * `createClient` from `@capacms/sdk/next`.
497
+ */
413
498
  export declare function createClient(config: CapaConfig): CapaClient;
package/dist/client.js CHANGED
@@ -19,6 +19,7 @@ exports.createClient = createClient;
19
19
  */
20
20
  const config_1 = require("./config");
21
21
  const http_1 = require("./http");
22
+ const key_family_1 = require("./next/key-family");
22
23
  const webhooks_1 = require("./webhooks");
23
24
  const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
24
25
  /**
@@ -64,15 +65,48 @@ function contentQuery(options = {}) {
64
65
  q[k] = v;
65
66
  return q;
66
67
  }
68
+ /**
69
+ * A content answer as a `Page`. A `structure=tree` answer (asked for through
70
+ * `where`) carries no relations map, and gets `{}`.
71
+ */
72
+ function pageOf(body, cacheTags) {
73
+ const rows = body.data ?? [];
74
+ return { data: rows, ids: rows.map(instanceIdOf), relations: body.relations ?? {}, meta: body.meta ?? {}, cacheTags };
75
+ }
76
+ /**
77
+ * `/v2` answers every `cap_` key "Invalid API key", which reads as a bad key
78
+ * rather than the wrong client, so one is refused here before the tenant id is
79
+ * asked for or anything is sent. The rule is the API's: `cap_`, case-sensitive.
80
+ */
81
+ function refuseCapKey(apiKey) {
82
+ if ((0, key_family_1.keyFamily)(apiKey) !== "cap")
83
+ return;
84
+ throw new TypeError('@capacms/sdk: this is the legacy /v2/api client, and cap_ keys read /api/: import { createClient } from "@capacms/sdk/next".');
85
+ }
86
+ /**
87
+ * The legacy `/v2/api` client, for a legacy key (`pk_`, `sk_` or unprefixed)
88
+ * and its tenant's id. For `/api/`, GraphQL and `cap_` keys, import
89
+ * `createClient` from `@capacms/sdk/next`.
90
+ */
67
91
  function createClient(config) {
92
+ refuseCapKey(config.apiKey);
68
93
  const resolved = (0, config_1.resolveConfig)(config);
69
94
  async function listContent(namespace, options = {}) {
70
95
  const { body, cacheTags } = await (0, http_1.getJson)(resolved, `/v2/api/${encodeURIComponent(namespace)}`, contentQuery(options));
71
- const rows = body.data ?? [];
72
- return { data: rows, ids: rows.map(instanceIdOf), meta: body.meta ?? {}, cacheTags };
96
+ return pageOf(body, cacheTags);
97
+ }
98
+ /** The first row of a page: alone, or with what the answer carried beside it. */
99
+ function one(page, options) {
100
+ const row = page.data[0];
101
+ if (row === undefined)
102
+ return null;
103
+ if (options.withRelations !== true)
104
+ return row;
105
+ return { data: row, id: page.ids[0] ?? null, relations: page.relations, meta: page.meta, cacheTags: page.cacheTags };
73
106
  }
74
107
  return {
75
108
  listContent,
109
+ // `Promise<any>` here and on findOne: CapaClient's overloads type the result by `withRelations`.
76
110
  async getContentById(namespace, id, options = {}) {
77
111
  // Fail here rather than spending a request on a guaranteed 400. See
78
112
  // instanceIdOf: for a shadowing model, the "id" on a listContent row is
@@ -83,8 +117,8 @@ function createClient(config) {
83
117
  `of \`row.id\`: the model "${namespace}" has a field named "id" that overwrites the ` +
84
118
  `instance id in that response. See #4628.`);
85
119
  }
86
- const { body } = await (0, http_1.getJson)(resolved, `/v2/api/${encodeURIComponent(namespace)}`, { ids: id, depth: options.depth, limit: 1 });
87
- return (body.data ?? [])[0] ?? null;
120
+ const { body, cacheTags } = await (0, http_1.getJson)(resolved, `/v2/api/${encodeURIComponent(namespace)}`, { ids: id, depth: options.depth, limit: 1 });
121
+ return one(pageOf(body, cacheTags), options);
88
122
  },
89
123
  /**
90
124
  * One row matched by field values — `findOne("shopify_page", {handle})`.
@@ -97,8 +131,9 @@ function createClient(config) {
97
131
  * the right fix, and belongs in Capa rather than here.
98
132
  */
99
133
  async findOne(namespace, where, options = {}) {
134
+ // `withRelations` rides along in the spread; contentQuery sends only the keys it knows.
100
135
  const page = await listContent(namespace, { ...options, where, limit: 1 });
101
- return page.data[0] ?? null;
136
+ return one(page, options);
102
137
  },
103
138
  async search(q, options = {}) {
104
139
  const text = String(q ?? "").trim();
package/dist/codegen.d.ts CHANGED
@@ -36,6 +36,7 @@
36
36
  * committed file stable under renames.
37
37
  */
38
38
  import { type CapaConfig } from "./config";
39
+ import type { CapaIntrospection } from "./next/graphql/introspection";
39
40
  export interface CodegenResult {
40
41
  /** False when the schema checksum was unchanged and nothing was fetched. */
41
42
  changed: boolean;
@@ -49,6 +50,8 @@ export interface SchemaModelField {
49
50
  type: string;
50
51
  arrayType?: string | null;
51
52
  relationRef?: string | null;
53
+ enumValues?: string[] | null;
54
+ required?: boolean | null;
52
55
  }
53
56
  export interface SchemaModel {
54
57
  id?: string;
@@ -60,6 +63,23 @@ export interface SchemaForTypes {
60
63
  }
61
64
  /** Read the checksum a previous run stamped into the generated file. */
62
65
  export declare function readStampedChecksum(existing: string | null | undefined): string | null;
66
+ /**
67
+ * The interface name for a name `/v2/schema/types` wrote. That route prints
68
+ * the PascalCase of the namespace whatever it holds, which is not always an
69
+ * identifier (`2024Events`, `Blog.posts`), and it is frozen legacy, so the
70
+ * file is made to parse here. The rule is GraphQL's (N1): every character
71
+ * outside [0-9A-Za-z] dropped and `_` before a leading digit, so
72
+ * `2024_events` is `_2024Events` in REST types and GraphQL types alike.
73
+ */
74
+ export declare function interfaceName(written: string): string;
75
+ /**
76
+ * Each model's interface name, by namespace. Two namespaces can give one
77
+ * PascalCase name (`twin_a` and `twin-a` are both `TwinA`), as can a model and
78
+ * another's `Select` or `Attrs` alias, or a shared declaration, and a module
79
+ * cannot declare an alias twice. Every model caught in such a collision is
80
+ * named by `namespaceInterface` instead, whatever order the models come in.
81
+ */
82
+ export declare function modelInterfaceNames(models: readonly SchemaModel[]): Map<string, string>;
63
83
  export declare function normalizeTypes(source: string, checksum: string | null, schema?: SchemaForTypes | null): string;
64
84
  export declare function typeNames(source: string): string[];
65
85
  /**
@@ -67,3 +87,38 @@ export declare function typeNames(source: string): string[];
67
87
  * checksum stamped in `existing`.
68
88
  */
69
89
  export declare function generate(config: CapaConfig, existing?: string | null): Promise<CodegenResult>;
90
+ /**
91
+ * `/v2/schema/types`'s type map and preamble, so a `cap_` key, which `/v2`
92
+ * does not take, gets the interfaces a legacy key gets. Copied from
93
+ * `apps/api/src/routes/v2/schema.ts`, which is frozen legacy; a test holds the
94
+ * two equal.
95
+ */
96
+ export declare const LEGACY_TYPE_MAP: Readonly<Record<string, string>>;
97
+ export declare const LEGACY_PREAMBLE = "export interface CapaImage {\n url: string;\n alt?: string;\n width?: number;\n height?: number;\n filesize?: number;\n filename?: string;\n}\n\nexport interface CapaVideo {\n url: string;\n thumbnail?: string;\n duration?: number;\n width?: number;\n height?: number;\n filesize?: number;\n filename?: string;\n}\n\nexport interface CapaFile {\n url: string;\n filename: string;\n filesize?: number;\n type?: string;\n}\n\nexport interface CapaInstance<T = Record<string, unknown>> {\n id: string;\n title?: string;\n slug?: string;\n data: T;\n status: 'draft' | 'published';\n createdAt: string;\n updatedAt: string;\n publishedAt?: string;\n}\n\n";
98
+ export interface RestTypesResult {
99
+ source: string;
100
+ /** Interface names present in the output, sorted. */
101
+ types: string[];
102
+ /**
103
+ * Models the key reads that the GraphQL schema leaves out, since their type
104
+ * names would collide (N1). The schema says nothing of their fields, so no
105
+ * interface is written for them.
106
+ */
107
+ restOnly: string[];
108
+ }
109
+ /**
110
+ * The REST model interfaces for a key's GraphQL schema: what
111
+ * `/v2/schema/types` and `/v2/schema` give a legacy key, for a `cap_` key.
112
+ * The model namespaces, field namespaces, Capa types and relation targets
113
+ * come from the schema's descriptions (N9), so the interfaces are keyed as
114
+ * REST reads them (`open-time`), not as GraphQL renames them (`open_time`).
115
+ *
116
+ * The text is written as the legacy route writes it and goes through
117
+ * `normalizeTypes`, the one place that turns that text into a module. Three
118
+ * things GraphQL does not say are written as it can: every field is optional,
119
+ * since the schema has no required flag; an enum is `string`, since it lists
120
+ * no values; and a field GraphQL leaves out (its description's `Not exposed:`
121
+ * line) is `unknown`. No checksum is stamped: `CAPA_SCHEMA_CHECKSUM` is the
122
+ * `/v2/schema` checksum, which a `cap_` key cannot read.
123
+ */
124
+ export declare function restTypesFromIntrospection(introspection: CapaIntrospection): RestTypesResult;