@localness/contract 0.4.1 → 0.4.3

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/README.md CHANGED
@@ -10,6 +10,17 @@ const meta: DeliveryMeta = deliveryMetaSchema.parse(await res.json())
10
10
 
11
11
  `zod` ^4 is a peer dependency.
12
12
 
13
+ ## Locale files
14
+
15
+ `@localness/contract/locale-file` reads and writes one-JSON-per-locale files exactly as Localness git-sync does: flat or nested keys, edits in place (key order, indentation, comments and line endings are kept), new keys appended. The `localness` CLI uses it, so a file written by either one is identical. It does not need `zod`.
16
+
17
+ ```ts
18
+ import { readLocaleFile, writeLocaleFile } from '@localness/contract/locale-file'
19
+
20
+ const { entries } = readLocaleFile(source)
21
+ const next = writeLocaleFile(source, { 'home.title': 'Welcome', 'old.key': null }, 'nested-json')
22
+ ```
23
+
13
24
  ## Versioning
14
25
 
15
26
  Independent of the Localness server version.
package/dist/index.d.mts CHANGED
@@ -127,12 +127,30 @@ export declare const deliveryKeysSchema: z.ZodObject<{
127
127
  }, z.core.$strip>;
128
128
  export type DeliveryKeys = z.infer<typeof deliveryKeysSchema>;
129
129
  export declare const deliveryLocaleStateNameSchema: z.ZodEnum<{
130
- outdated: "outdated";
131
130
  draft: "draft";
132
131
  published: "published";
132
+ outdated: "outdated";
133
133
  unpublished: "unpublished";
134
134
  }>;
135
135
  export type DeliveryLocaleStateName = z.infer<typeof deliveryLocaleStateNameSchema>;
136
+ /** One key as `/keys/states` lists it; the key write routes answer with it too. */
137
+ export declare const deliveryKeyStateSchema: z.ZodObject<{
138
+ id: z.ZodNumber;
139
+ key: z.ZodString;
140
+ releaseIds: z.ZodArray<z.ZodNumber>;
141
+ draft: z.ZodBoolean;
142
+ locales: z.ZodRecord<z.ZodString, z.ZodObject<{
143
+ draftText: z.ZodNullable<z.ZodString>;
144
+ publishedText: z.ZodNullable<z.ZodString>;
145
+ state: z.ZodEnum<{
146
+ draft: "draft";
147
+ published: "published";
148
+ outdated: "outdated";
149
+ unpublished: "unpublished";
150
+ }>;
151
+ }, z.core.$strip>>;
152
+ }, z.core.$strip>;
153
+ export type DeliveryKeyState = z.infer<typeof deliveryKeyStateSchema>;
136
154
  /**
137
155
  * `GET /api/v1/keys/states` — per-key, per-locale state, drafts included.
138
156
  * Unlike every other delivery shape this carries draft text: a read token sees
@@ -149,9 +167,9 @@ export declare const deliveryKeyStatesSchema: z.ZodObject<{
149
167
  draftText: z.ZodNullable<z.ZodString>;
150
168
  publishedText: z.ZodNullable<z.ZodString>;
151
169
  state: z.ZodEnum<{
152
- outdated: "outdated";
153
170
  draft: "draft";
154
171
  published: "published";
172
+ outdated: "outdated";
155
173
  unpublished: "unpublished";
156
174
  }>;
157
175
  }, z.core.$strip>>;
@@ -334,4 +352,94 @@ export declare const capturePageSchema: z.ZodObject<{
334
352
  releaseIds: z.ZodArray<z.ZodNumber>;
335
353
  }, z.core.$strip>>;
336
354
  }, z.core.$strip>;
337
- export type CapturePage = z.infer<typeof capturePageSchema>;
355
+ export type CapturePage = z.infer<typeof capturePageSchema>;
356
+ /**
357
+ * Response shapes of the key write routes an IDE plugin uses: create a key from
358
+ * text in code, name it, and edit its drafts. Same versioning rule as
359
+ * `delivery.ts`. A key created with a name publishes its source text; every
360
+ * other write lands in drafts, and publishing stays in the web app.
361
+ */
362
+ /**
363
+ * `POST /api/v1/keys` and `PATCH /api/v1/keys`. The key comes back in the
364
+ * `/keys/states` shape, so a plugin can drop it straight into that cache.
365
+ */
366
+ export declare const keyWriteResultSchema: z.ZodObject<{
367
+ sourceLocale: z.ZodString;
368
+ created: z.ZodBoolean;
369
+ key: z.ZodObject<{
370
+ id: z.ZodNumber;
371
+ key: z.ZodString;
372
+ releaseIds: z.ZodArray<z.ZodNumber>;
373
+ draft: z.ZodBoolean;
374
+ locales: z.ZodRecord<z.ZodString, z.ZodObject<{
375
+ draftText: z.ZodNullable<z.ZodString>;
376
+ publishedText: z.ZodNullable<z.ZodString>;
377
+ state: z.ZodEnum<{
378
+ draft: "draft";
379
+ published: "published";
380
+ outdated: "outdated";
381
+ unpublished: "unpublished";
382
+ }>;
383
+ }, z.core.$strip>>;
384
+ }, z.core.$strip>;
385
+ }, z.core.$strip>;
386
+ export type KeyWriteResult = z.infer<typeof keyWriteResultSchema>;
387
+ /**
388
+ * The `data` of a `PATCH /api/v1/keys` 409: a locale's draft no longer matches
389
+ * the `expected` the client sent. Nothing was written; `key` is the current
390
+ * state to diff against before retrying.
391
+ */
392
+ export declare const keyDraftConflictSchema: z.ZodObject<{
393
+ locales: z.ZodArray<z.ZodString>;
394
+ key: z.ZodObject<{
395
+ id: z.ZodNumber;
396
+ key: z.ZodString;
397
+ releaseIds: z.ZodArray<z.ZodNumber>;
398
+ draft: z.ZodBoolean;
399
+ locales: z.ZodRecord<z.ZodString, z.ZodObject<{
400
+ draftText: z.ZodNullable<z.ZodString>;
401
+ publishedText: z.ZodNullable<z.ZodString>;
402
+ state: z.ZodEnum<{
403
+ draft: "draft";
404
+ published: "published";
405
+ outdated: "outdated";
406
+ unpublished: "unpublished";
407
+ }>;
408
+ }, z.core.$strip>>;
409
+ }, z.core.$strip>;
410
+ }, z.core.$strip>;
411
+ export type KeyDraftConflict = z.infer<typeof keyDraftConflictSchema>;
412
+ export declare const keyViolationSchema: z.ZodEnum<{
413
+ prefix: "prefix";
414
+ charset: "charset";
415
+ segment: "segment";
416
+ case: "case";
417
+ depth: "depth";
418
+ }>;
419
+ /**
420
+ * `POST /api/v1/keys/suggest-name` — the web app's AI key naming, under the
421
+ * project's key convention. `key` is empty when the text has nothing nameable.
422
+ */
423
+ export declare const keyNameSuggestionSchema: z.ZodObject<{
424
+ source: z.ZodString;
425
+ key: z.ZodString;
426
+ confidence: z.ZodOptional<z.ZodNumber>;
427
+ alternatives: z.ZodOptional<z.ZodArray<z.ZodObject<{
428
+ key: z.ZodString;
429
+ confidence: z.ZodOptional<z.ZodNumber>;
430
+ }, z.core.$strip>>>;
431
+ reason: z.ZodOptional<z.ZodString>;
432
+ violations: z.ZodArray<z.ZodEnum<{
433
+ prefix: "prefix";
434
+ charset: "charset";
435
+ segment: "segment";
436
+ case: "case";
437
+ depth: "depth";
438
+ }>>;
439
+ duplicates: z.ZodArray<z.ZodObject<{
440
+ key: z.ZodString;
441
+ sourceText: z.ZodString;
442
+ sameSourceText: z.ZodBoolean;
443
+ }, z.core.$strip>>;
444
+ }, z.core.$strip>;
445
+ export type KeyNameSuggestion = z.infer<typeof keyNameSuggestionSchema>;
package/dist/index.mjs CHANGED
@@ -134,6 +134,19 @@ const deliveryLocaleStateNameSchema = z.enum([
134
134
  "outdated",
135
135
  "unpublished"
136
136
  ]);
137
+ /** One key as `/keys/states` lists it; the key write routes answer with it too. */
138
+ const deliveryKeyStateSchema = z.object({
139
+ id: z.number(),
140
+ key: z.string(),
141
+ releaseIds: z.array(z.number()),
142
+ /** True while the key still has unpublished edits or was never published. */
143
+ draft: z.boolean(),
144
+ locales: z.record(z.string(), z.object({
145
+ draftText: z.string().nullable(),
146
+ publishedText: z.string().nullable(),
147
+ state: deliveryLocaleStateNameSchema
148
+ }))
149
+ });
137
150
  /**
138
151
  * `GET /api/v1/keys/states` — per-key, per-locale state, drafts included.
139
152
  * Unlike every other delivery shape this carries draft text: a read token sees
@@ -141,18 +154,7 @@ const deliveryLocaleStateNameSchema = z.enum([
141
154
  */
142
155
  const deliveryKeyStatesSchema = z.object({
143
156
  sourceLocale: z.string(),
144
- keys: z.array(z.object({
145
- id: z.number(),
146
- key: z.string(),
147
- releaseIds: z.array(z.number()),
148
- /** True while the key still has unpublished edits or was never published. */
149
- draft: z.boolean(),
150
- locales: z.record(z.string(), z.object({
151
- draftText: z.string().nullable(),
152
- publishedText: z.string().nullable(),
153
- state: deliveryLocaleStateNameSchema
154
- }))
155
- })),
157
+ keys: z.array(deliveryKeyStateSchema),
156
158
  /** Keys in scope, before paging. */
157
159
  total: z.number(),
158
160
  offset: z.number(),
@@ -254,4 +256,62 @@ const capturePageSchema = z.object({
254
256
  releaseIds: z.array(z.number()),
255
257
  tags: z.array(captureTagSchema)
256
258
  });
257
- export { captureKeyHitSchema, captureKeySearchSchema, captureMatchSchema, capturePageSchema, captureTagSchema, createPageResultSchema, deleteTagsResultSchema, deliveryBundleSchema, deliveryKeyStatesSchema, deliveryKeysSchema, deliveryLocaleSchema, deliveryLocaleStateNameSchema, deliveryMetaSchema, deliveryOutdatedSchema, deliveryProjectRefSchema, deliveryProjectsSchema, i18nKeyNamespaceCountsSchema, idSchema, importPageResultSchema, importPageTagsSchema, importedTagSchema, keyNamespaceSchema, projectReleaseSchema, releaseMembershipResultSchema, uploadResultSchema };
259
+ // ../../shared/contract/keys.ts
260
+ /**
261
+ * Response shapes of the key write routes an IDE plugin uses: create a key from
262
+ * text in code, name it, and edit its drafts. Same versioning rule as
263
+ * `delivery.ts`. A key created with a name publishes its source text; every
264
+ * other write lands in drafts, and publishing stays in the web app.
265
+ */
266
+ /**
267
+ * `POST /api/v1/keys` and `PATCH /api/v1/keys`. The key comes back in the
268
+ * `/keys/states` shape, so a plugin can drop it straight into that cache.
269
+ */
270
+ const keyWriteResultSchema = z.object({
271
+ sourceLocale: z.string(),
272
+ /**
273
+ * False when the request named no key and the text already has its
274
+ * `__unnamed_` key — the same text maps to the same placeholder. Always false
275
+ * for `PATCH`.
276
+ */
277
+ created: z.boolean(),
278
+ key: deliveryKeyStateSchema
279
+ });
280
+ /**
281
+ * The `data` of a `PATCH /api/v1/keys` 409: a locale's draft no longer matches
282
+ * the `expected` the client sent. Nothing was written; `key` is the current
283
+ * state to diff against before retrying.
284
+ */
285
+ const keyDraftConflictSchema = z.object({
286
+ locales: z.array(z.string()),
287
+ key: deliveryKeyStateSchema
288
+ });
289
+ const keyViolationSchema = z.enum([
290
+ "prefix",
291
+ "charset",
292
+ "segment",
293
+ "case",
294
+ "depth"
295
+ ]);
296
+ /**
297
+ * `POST /api/v1/keys/suggest-name` — the web app's AI key naming, under the
298
+ * project's key convention. `key` is empty when the text has nothing nameable.
299
+ */
300
+ const keyNameSuggestionSchema = z.object({
301
+ source: z.string(),
302
+ key: z.string(),
303
+ confidence: z.number().optional(),
304
+ alternatives: z.array(z.object({
305
+ key: z.string(),
306
+ confidence: z.number().optional()
307
+ })).optional(),
308
+ reason: z.string().optional(),
309
+ violations: z.array(keyViolationSchema),
310
+ /** Candidates the project already has; `sameSourceText` means reuse, not clash. */
311
+ duplicates: z.array(z.object({
312
+ key: z.string(),
313
+ sourceText: z.string(),
314
+ sameSourceText: z.boolean()
315
+ }))
316
+ });
317
+ export { captureKeyHitSchema, captureKeySearchSchema, captureMatchSchema, capturePageSchema, captureTagSchema, createPageResultSchema, deleteTagsResultSchema, deliveryBundleSchema, deliveryKeyStateSchema, deliveryKeyStatesSchema, deliveryKeysSchema, deliveryLocaleSchema, deliveryLocaleStateNameSchema, deliveryMetaSchema, deliveryOutdatedSchema, deliveryProjectRefSchema, deliveryProjectsSchema, i18nKeyNamespaceCountsSchema, idSchema, importPageResultSchema, importPageTagsSchema, importedTagSchema, keyDraftConflictSchema, keyNameSuggestionSchema, keyNamespaceSchema, keyViolationSchema, keyWriteResultSchema, projectReleaseSchema, releaseMembershipResultSchema, uploadResultSchema };
@@ -0,0 +1,61 @@
1
+ /**
2
+ * One JSON file per locale, as git-sync's `locale-files` layout and the
3
+ * `localness` CLI read and write it. Both go through this module, so a file
4
+ * written by either one is byte-for-byte what the other would write.
5
+ *
6
+ * Package subpath `@localness/contract/locale-file`.
7
+ */
8
+ export declare const LOCALE_PLACEHOLDER = "{locale}";
9
+ export declare const DEFAULT_LOCALE_FILE_PATTERN = "{locale}.json";
10
+ /** Nested JSON joins its path with this. */
11
+ export declare const LOCALE_KEY_SEPARATOR = ".";
12
+ /** Key layout a write uses for a new key. Reading accepts both. */
13
+ export declare const LocaleFileFormat: {
14
+ readonly FLAT_JSON: "flat-json";
15
+ readonly NESTED_JSON: "nested-json";
16
+ };
17
+ export type LocaleFileFormat = (typeof LocaleFileFormat)[keyof typeof LocaleFileFormat];
18
+ /**
19
+ * The text to store, or null for "no translation". Only line endings and the
20
+ * ends are touched: inner whitespace is copy.
21
+ */
22
+ export declare function normalizeLocaleText(value: unknown): string | null;
23
+ /** No prototype, so a key named `__proto__` is stored like any other. */
24
+ export declare function emptyEntries(): Record<string, string>;
25
+ /** Error text for an unusable directory / pattern, or null. */
26
+ export declare function localeFilesScopeError(dir: string, pattern: string): string | null;
27
+ /**
28
+ * `zh_cn` → `zh-CN`, `en` → `en`: the BCP 47 shape app repos name files by.
29
+ * An explicit `localeMap` entry wins.
30
+ */
31
+ export declare function localeFileName(locale: string, localeMap: Record<string, string> | null): string;
32
+ /** `/`-separated path of a locale's file. `dir` must pass `localeFilesScopeError`. */
33
+ export declare function localeFilePath(dir: string, pattern: string, locale: string, localeMap: Record<string, string> | null): string;
34
+ /**
35
+ * Keys are read the same way Translations import reads them — nested objects
36
+ * joined with `.` — and every key remembers the property path it came from, so
37
+ * a write lands on that exact property even in a file that mixes flat and
38
+ * nested keys.
39
+ */
40
+ export type LocaleFileContent = {
41
+ entries: Record<string, string>;
42
+ paths: Map<string, string[]>;
43
+ /** Paths holding something other than an object or text (arrays, null). */
44
+ blocked: string[][];
45
+ };
46
+ export declare function readLocaleFile(source: string): LocaleFileContent;
47
+ /**
48
+ * Keys a write cannot place. A key already in the file is always writable, in
49
+ * place. A new nested key is not when its path runs into an existing value
50
+ * (`a` is text, so `a.b` has nowhere to go), would replace an object, has an
51
+ * empty segment, or collides that way with another new key — then neither of
52
+ * the two is written, since there is no right one to pick.
53
+ */
54
+ export declare function unwritableKeys(file: LocaleFileContent, keys: string[], format: LocaleFileFormat): Set<string>;
55
+ /**
56
+ * Sets each entry in place and leaves everything else byte-for-byte: key
57
+ * order, indentation, comments and line endings. A new key goes to the end of
58
+ * its parent object. A `null` value removes the key. `source` null creates the
59
+ * file. Callers must drop `unwritableKeys` first.
60
+ */
61
+ export declare function writeLocaleFile(source: string | null, entries: Record<string, string | null>, format: LocaleFileFormat): string;