@open-dpp/dto 2.10.0 → 3.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.
Files changed (42) hide show
  1. package/dist/index.d.ts +4 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +4 -0
  4. package/dist/permalinks/permalink.dto.d.ts +3690 -9
  5. package/dist/permalinks/permalink.dto.d.ts.map +1 -1
  6. package/dist/permalinks/permalink.dto.js +89 -6
  7. package/dist/shared/permalink-base-url.schema.d.ts +1 -0
  8. package/dist/shared/permalink-base-url.schema.d.ts.map +1 -1
  9. package/dist/shared/permalink-base-url.schema.js +9 -0
  10. package/dist/unique-product-identifiers/create-internal-unique-product-identifier.dto.d.ts +6 -0
  11. package/dist/unique-product-identifiers/create-internal-unique-product-identifier.dto.d.ts.map +1 -0
  12. package/dist/unique-product-identifiers/create-internal-unique-product-identifier.dto.js +9 -0
  13. package/dist/unique-product-identifiers/create-unique-product-identifier.dto.d.ts +9 -0
  14. package/dist/unique-product-identifiers/create-unique-product-identifier.dto.d.ts.map +1 -0
  15. package/dist/unique-product-identifiers/create-unique-product-identifier.dto.js +13 -0
  16. package/dist/unique-product-identifiers/gs1/gs1-ai-description.d.ts +6 -4
  17. package/dist/unique-product-identifiers/gs1/gs1-ai-description.d.ts.map +1 -1
  18. package/dist/unique-product-identifiers/gs1/gs1-ai-description.js +13 -3
  19. package/dist/unique-product-identifiers/gs1/gs1-digital-link.d.ts +22 -5
  20. package/dist/unique-product-identifiers/gs1/gs1-digital-link.d.ts.map +1 -1
  21. package/dist/unique-product-identifiers/gs1/gs1-digital-link.js +33 -12
  22. package/dist/unique-product-identifiers/unique-product-identifier-list-item.dto.d.ts +104 -0
  23. package/dist/unique-product-identifiers/unique-product-identifier-list-item.dto.d.ts.map +1 -0
  24. package/dist/unique-product-identifiers/unique-product-identifier-list-item.dto.js +42 -0
  25. package/dist/unique-product-identifiers/unique-product-identifier-type.d.ts +1 -1
  26. package/dist/unique-product-identifiers/update-unique-product-identifier.dto.d.ts +8 -0
  27. package/dist/unique-product-identifiers/update-unique-product-identifier.dto.d.ts.map +1 -0
  28. package/dist/unique-product-identifiers/update-unique-product-identifier.dto.js +12 -0
  29. package/package.json +1 -1
  30. package/src/index.ts +4 -0
  31. package/src/permalinks/permalink.dto.spec.ts +469 -0
  32. package/src/permalinks/permalink.dto.ts +103 -5
  33. package/src/shared/permalink-base-url.schema.spec.ts +17 -1
  34. package/src/shared/permalink-base-url.schema.ts +8 -0
  35. package/src/unique-product-identifiers/create-internal-unique-product-identifier.dto.ts +11 -0
  36. package/src/unique-product-identifiers/create-unique-product-identifier.dto.ts +15 -0
  37. package/src/unique-product-identifiers/gs1/gs1-ai-description.spec.ts +14 -0
  38. package/src/unique-product-identifiers/gs1/gs1-ai-description.ts +16 -5
  39. package/src/unique-product-identifiers/gs1/gs1-digital-link.spec.ts +17 -1
  40. package/src/unique-product-identifiers/gs1/gs1-digital-link.ts +35 -11
  41. package/src/unique-product-identifiers/unique-product-identifier-list-item.dto.ts +64 -0
  42. package/src/unique-product-identifiers/update-unique-product-identifier.dto.ts +14 -0
@@ -1,4 +1,4 @@
1
- import { PermalinkBaseUrlSchema } from "./permalink-base-url.schema";
1
+ import { baseUrlOrigin, PermalinkBaseUrlSchema } from "./permalink-base-url.schema";
2
2
  import { LatestApiVersionWithPrefixDto } from "../api-version.dto";
3
3
 
4
4
  describe("PermalinkBaseUrlSchema", () => {
@@ -95,3 +95,19 @@ describe("PermalinkBaseUrlSchema", () => {
95
95
  });
96
96
  });
97
97
  });
98
+
99
+ describe("baseUrlOrigin", () => {
100
+ it.each([
101
+ ["strips the presentation /p path", "https://example.com/p", "https://example.com"],
102
+ ["leaves a bare origin unchanged", "https://example.com", "https://example.com"],
103
+ ["preserves a non-default port", "http://localhost:3000/p", "http://localhost:3000"],
104
+ ["strips a nested path", "https://example.com/dpp/v1", "https://example.com"],
105
+ ["lowercases the host", "https://Example.COM/p", "https://example.com"],
106
+ ])("%s", (_label: string, input: string, expected: string) => {
107
+ expect(baseUrlOrigin(input)).toBe(expected);
108
+ });
109
+
110
+ it("returns the input unchanged when it cannot be parsed as a URL", () => {
111
+ expect(baseUrlOrigin("not a url")).toBe("not a url");
112
+ });
113
+ });
@@ -12,6 +12,14 @@ export function canonicaliseBaseUrl(s: string): string {
12
12
  }
13
13
  }
14
14
 
15
+ export function baseUrlOrigin(s: string): string {
16
+ try {
17
+ return new URL(s).origin;
18
+ } catch {
19
+ return s;
20
+ }
21
+ }
22
+
15
23
  export const PermalinkBaseUrlSchema = z
16
24
  .string()
17
25
  .min(8)
@@ -0,0 +1,11 @@
1
+ import { z } from "zod";
2
+
3
+ export const CreateInternalUniqueProductIdentifierRequestSchema = z
4
+ .object({
5
+ referenceId: z.uuid(),
6
+ })
7
+ .meta({ id: "CreateInternalUniqueProductIdentifierRequest" });
8
+
9
+ export type CreateInternalUniqueProductIdentifierRequest = z.infer<
10
+ typeof CreateInternalUniqueProductIdentifierRequestSchema
11
+ >;
@@ -0,0 +1,15 @@
1
+ import { z } from "zod";
2
+ import { Cset82ComponentInputSchema, GtinInputSchema } from "./gs1/gs1-digital-link";
3
+
4
+ export const CreateGs1UniqueProductIdentifierRequestSchema = z
5
+ .object({
6
+ referenceId: z.uuid(),
7
+ gtin: GtinInputSchema,
8
+ batch: Cset82ComponentInputSchema.optional(),
9
+ serial: Cset82ComponentInputSchema.optional(),
10
+ })
11
+ .meta({ id: "CreateGs1UniqueProductIdentifierRequest" });
12
+
13
+ export type CreateGs1UniqueProductIdentifierRequest = z.infer<
14
+ typeof CreateGs1UniqueProductIdentifierRequestSchema
15
+ >;
@@ -49,6 +49,20 @@ describe("getGs1AiDescription", () => {
49
49
  }
50
50
  });
51
51
 
52
+ it("accepts BCP 47 tags and casing variants (primary subtag wins)", () => {
53
+ expect(getGs1AiDescription("17", "de-DE")).toBe("Verfallsdatum (JJMMTT)");
54
+ expect(getGs1AiDescription("17", "DE")).toBe("Verfallsdatum (JJMMTT)");
55
+ expect(getGs1AiDescription("17", "de-CH")).toBe("Verfallsdatum (JJMMTT)");
56
+ });
57
+
58
+ it("falls back to English for languages not vendored", () => {
59
+ expect(getGs1AiDescription("17", "fr")).toBe("Expiration date (YYMMDD)");
60
+ expect(getGs1AiDescription("17", "fr-FR")).toBe("Expiration date (YYMMDD)");
61
+ expect(getGs1AiDescription("17", "")).toBe("Expiration date (YYMMDD)");
62
+ // Prototype names must not leak object internals as a "translation".
63
+ expect(getGs1AiDescription("17", "constructor")).toBe("Expiration date (YYMMDD)");
64
+ });
65
+
52
66
  it("returns undefined for unknown or non-AI input", () => {
53
67
  expect(getGs1AiDescription("0000", "en")).toBeUndefined();
54
68
  expect(getGs1AiDescription("", "en")).toBeUndefined();
@@ -1,4 +1,4 @@
1
- import { GS1_AI_I18N, type Gs1AiI18nLang } from "./gs1-ai-i18n";
1
+ import { GS1_AI_I18N, type Gs1AiI18nEntry } from "./gs1-ai-i18n";
2
2
 
3
3
  /** A syntactically valid GS1 Application Identifier: 2-4 digits. */
4
4
  const AI_SHAPE = /^\d{2,4}$/;
@@ -21,26 +21,37 @@ function candidateKeys(ai: string): string[] {
21
21
  return keys;
22
22
  }
23
23
 
24
+ /** Resolve a language cell by BCP 47 primary subtag, falling back to English. */
25
+ function pickLanguageCell(entry: Gs1AiI18nEntry, lang: string): string {
26
+ const primary = lang.split("-")[0]?.toLowerCase() ?? "";
27
+ // Own enumerable entries only — a tag like "constructor" must not reach the prototype.
28
+ const cell: string | undefined = Object.entries(entry).find(([key]) => key === primary)?.[1];
29
+ return cell ?? entry.en;
30
+ }
31
+
24
32
  /**
25
33
  * Resolve the translated description for a GS1 Application Identifier.
26
34
  *
27
35
  * Lookup chain: exact AI key -> family key ("n"/"s" collapse) -> "91-99"
28
- * range key; within the found row, the requested language falls back to
29
- * English (every vendored row carries English text).
36
+ * range key. `lang` accepts any BCP 47-ish tag ("de", "de-DE", "DE"); its
37
+ * primary subtag selects the vendored translation, and languages GS1 data
38
+ * doesn't carry fall back to English (every vendored row has English text).
39
+ * Which languages are vendored is an internal detail of this module — app
40
+ * display languages grow independently.
30
41
  *
31
42
  * Returns `undefined` for unknown AIs and non-AI input — callers fall back to
32
43
  * the English `title` from GS1_AI_TABLE as the last resort.
33
44
  *
34
45
  * Pure function, no I/O, no mutation.
35
46
  */
36
- export function getGs1AiDescription(ai: string, lang: Gs1AiI18nLang): string | undefined {
47
+ export function getGs1AiDescription(ai: string, lang: string): string | undefined {
37
48
  if (!AI_SHAPE.test(ai)) {
38
49
  return undefined;
39
50
  }
40
51
  for (const key of candidateKeys(ai)) {
41
52
  const entry = GS1_AI_I18N[key];
42
53
  if (entry !== undefined) {
43
- return entry[lang] ?? entry.en;
54
+ return pickLanguageCell(entry, lang);
44
55
  }
45
56
  }
46
57
  return undefined;
@@ -3,6 +3,7 @@ import { GS1_AI_TABLE, type Gs1AiTableEntry } from "./gs1-ai-table";
3
3
  import {
4
4
  buildGs1DataAttributeQuery,
5
5
  buildGs1DigitalLink,
6
+ buildGs1DigitalLinkPath,
6
7
  Cset82ComponentInputSchema,
7
8
  Cset82ComponentSchema,
8
9
  GS1_DATA_ATTRIBUTE_MAX_LENGTH,
@@ -104,7 +105,7 @@ describe("RFC 3986 canonical encoding", () => {
104
105
  gtin: "04006381333931",
105
106
  serial: "A!'()*B",
106
107
  });
107
- expect(link).toBe("https://r.example/01/04006381333931/21/A%21%27%28%29%2AB");
108
+ expect(link).toBe("https://r.example/gs1/v1/01/04006381333931/21/A%21%27%28%29%2AB");
108
109
  });
109
110
 
110
111
  it("percent-encodes !'()* in data-attribute query values", () => {
@@ -113,6 +114,21 @@ describe("RFC 3986 canonical encoding", () => {
113
114
  });
114
115
  });
115
116
 
117
+ describe("buildGs1DigitalLinkPath", () => {
118
+ it("builds the base-less canonical path with normalization and encoding", () => {
119
+ expect(buildGs1DigitalLinkPath({ gtin: "4006381333931", batch: "LOT/1", serial: "SN-1" })).toBe(
120
+ "01/04006381333931/10/LOT%2F1/21/SN-1",
121
+ );
122
+ });
123
+
124
+ it("matches the path portion of buildGs1DigitalLink behind the resolver prefix", () => {
125
+ const parts = { gtin: "04006381333931", batch: "B(1)", serial: "S:1" };
126
+ expect(buildGs1DigitalLink("https://r.example", parts)).toBe(
127
+ `https://r.example/gs1/v1/${buildGs1DigitalLinkPath(parts)}`,
128
+ );
129
+ });
130
+ });
131
+
116
132
  describe("non-string input guards (plain-JS callers)", () => {
117
133
  it("isGs1DataAttributeAi rejects non-string AI keys", () => {
118
134
  expect(isGs1DataAttributeAi(17 as unknown as string)).toBe(false);
@@ -311,33 +311,57 @@ function normalizeOptionalComponent(
311
311
  return trimmed;
312
312
  }
313
313
 
314
+ /**
315
+ * Path prefix (no leading/trailing slash) under which the GS1 resolver endpoint
316
+ * is mounted, between the resolver base and the `01/...` identity path.
317
+ * Must match the `Gs1ResolverController` route mounting in the backend.
318
+ */
319
+ export const GS1_RESOLVER_PATH_PREFIX = "gs1/v1";
320
+
314
321
  /**
315
322
  * Build the uncompressed, canonical GS1 Digital Link for a GS1 identity.
316
323
  *
317
- * Emits the GTIN path and, when present, appends the batch (AI `10`) and serial
318
- * (AI `21`) segments in canonical GS1 order `01 -> 10 -> 21`. A blank batch/serial
319
- * is treated as absent. The resolver base is canonicalised (host lowercased,
320
- * trailing slash dropped) and any path it carries is preserved; batch/serial
321
- * values are percent-encoded for the path.
324
+ * Emits the resolver prefix (`gs1/v1`) followed by the GTIN path and, when
325
+ * present, the batch (AI `10`) and serial (AI `21`) segments in canonical GS1
326
+ * order `01 -> 10 -> 21`. A blank batch/serial is treated as absent. The
327
+ * resolver base is canonicalised (host lowercased, trailing slash dropped) and
328
+ * any path it carries is preserved; batch/serial values are percent-encoded
329
+ * for the path.
322
330
  *
323
331
  * @throws Error when the GTIN is invalid or a batch/serial violates CSET-82 /
324
332
  * length / the dot-segment rule.
325
333
  */
326
334
  export function buildGs1DigitalLink(resolverBase: string, parts: Gs1DigitalLinkParts): string {
335
+ const base = canonicaliseBaseUrl(resolverBase);
336
+ let url = `${base}/${GS1_RESOLVER_PATH_PREFIX}/${buildGs1DigitalLinkPath(parts)}`;
337
+ url += buildGs1DataAttributeQuery(parts.dataAttributes);
338
+ return url;
339
+ }
340
+
341
+ /**
342
+ * Build the base-less canonical GS1 Digital Link path
343
+ * `01/{gtin14}[/10/{batch}][/21/{serial}]` (no leading slash, no query string).
344
+ * Same normalization, validation, and percent-encoding as `buildGs1DigitalLink`;
345
+ * usable as an exact-match canonical value for a GS1 identity.
346
+ *
347
+ * @throws Error when the GTIN is invalid or a batch/serial violates CSET-82 /
348
+ * length / the dot-segment rule.
349
+ */
350
+ export function buildGs1DigitalLinkPath(
351
+ parts: Omit<Gs1DigitalLinkParts, "dataAttributes">,
352
+ ): string {
327
353
  const gtin14 = normalizeToGtin14(parts.gtin);
328
354
  const batch = normalizeOptionalComponent(parts.batch, "Batch");
329
355
  const serial = normalizeOptionalComponent(parts.serial, "Serial");
330
- const base = canonicaliseBaseUrl(resolverBase);
331
356
 
332
- let url = `${base}/${GS1_AI_GTIN}/${gtin14}`;
357
+ let path = `${GS1_AI_GTIN}/${gtin14}`;
333
358
  if (batch !== undefined) {
334
- url += `/${GS1_AI_BATCH}/${encodeRfc3986(batch)}`;
359
+ path += `/${GS1_AI_BATCH}/${encodeRfc3986(batch)}`;
335
360
  }
336
361
  if (serial !== undefined) {
337
- url += `/${GS1_AI_SERIAL}/${encodeRfc3986(serial)}`;
362
+ path += `/${GS1_AI_SERIAL}/${encodeRfc3986(serial)}`;
338
363
  }
339
- url += buildGs1DataAttributeQuery(parts.dataAttributes);
340
- return url;
364
+ return path;
341
365
  }
342
366
 
343
367
  /**
@@ -0,0 +1,64 @@
1
+ import { z } from "zod";
2
+ import {
3
+ Gs1GranularitySchema,
4
+ UniqueProductIdentifierTypeSchema,
5
+ } from "./unique-product-identifier-type";
6
+ import { Cset82ComponentSchema, Gtin14Schema } from "./gs1/gs1-digital-link";
7
+ import { PermalinkKindSchema } from "../permalinks/permalink.dto";
8
+ import { PagingMetadataDtoSchema } from "../shared/pagination.dto";
9
+
10
+ export const UniqueProductIdentifierPermalinkSummaryDtoSchema = z
11
+ .object({
12
+ id: z.uuid(),
13
+ kind: PermalinkKindSchema,
14
+ publicUrl: z.string().url(),
15
+ })
16
+ .meta({ id: "UniqueProductIdentifierPermalinkSummary" });
17
+
18
+ export type UniqueProductIdentifierPermalinkSummaryDto = z.infer<
19
+ typeof UniqueProductIdentifierPermalinkSummaryDtoSchema
20
+ >;
21
+
22
+ export const UniqueProductIdentifierListItemDtoSchema = z
23
+ .object({
24
+ uuid: z.uuid(),
25
+ referenceId: z.uuid(),
26
+ type: UniqueProductIdentifierTypeSchema,
27
+ gtin: Gtin14Schema.nullable(),
28
+ batch: Cset82ComponentSchema.nullish().overwrite((v) => v ?? null),
29
+ serial: Cset82ComponentSchema.nullable()
30
+ .nullish()
31
+ .overwrite((v) => v ?? null),
32
+ granularity: Gs1GranularitySchema.nullable(),
33
+ digitalLink: z
34
+ .string()
35
+ .url()
36
+ .nullish()
37
+ .overwrite((v) => v ?? null),
38
+ passportPublished: z.boolean(),
39
+ permalink: UniqueProductIdentifierPermalinkSummaryDtoSchema.nullish().overwrite(
40
+ (v) => v ?? null,
41
+ ),
42
+ })
43
+ .meta({ id: "UniqueProductIdentifierListItem" });
44
+
45
+ export type UniqueProductIdentifierListItemDto = z.infer<
46
+ typeof UniqueProductIdentifierListItemDtoSchema
47
+ >;
48
+
49
+ export const UniqueProductIdentifierListDtoSchema = z.array(
50
+ UniqueProductIdentifierListItemDtoSchema,
51
+ );
52
+
53
+ export type UniqueProductIdentifierListDto = z.infer<typeof UniqueProductIdentifierListDtoSchema>;
54
+
55
+ export const UniqueProductIdentifierPaginationDtoSchema = z
56
+ .object({
57
+ ...PagingMetadataDtoSchema.shape,
58
+ result: UniqueProductIdentifierListItemDtoSchema.array(),
59
+ })
60
+ .meta({ id: "UniqueProductIdentifiers" });
61
+
62
+ export type UniqueProductIdentifierPaginationDto = z.infer<
63
+ typeof UniqueProductIdentifierPaginationDtoSchema
64
+ >;
@@ -0,0 +1,14 @@
1
+ import { z } from "zod";
2
+ import { Cset82ComponentInputSchema, GtinInputSchema } from "./gs1/gs1-digital-link";
3
+
4
+ export const UpdateGs1UniqueProductIdentifierRequestSchema = z
5
+ .object({
6
+ gtin: GtinInputSchema,
7
+ batch: Cset82ComponentInputSchema.optional(),
8
+ serial: Cset82ComponentInputSchema.optional(),
9
+ })
10
+ .meta({ id: "UpdateGs1UniqueProductIdentifierRequest" });
11
+
12
+ export type UpdateGs1UniqueProductIdentifierRequest = z.infer<
13
+ typeof UpdateGs1UniqueProductIdentifierRequestSchema
14
+ >;