@node-zugferd/utils 0.0.0-stage → 1.0.0-beta.2

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/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ The MIT License (MIT) Copyright (c) 2025 - present, Joél de Olivèira Solano da Silva
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,74 @@
1
+ import { InferInput, InferOutput, MergeProfileInput } from "@node-zugferd/data-types";
2
+ import { BaseIssue, BaseSchema, BaseSchemaAsync } from "@node-zugferd/core/data-types";
3
+ import { MarkOptional, XMLBuilder, ZugferdProfile, ZugferdProfileBuildContext, ZugferdProfileRulesContext } from "@node-zugferd/core/types";
4
+ export declare const base64: {
5
+ encode: (data: string | Uint8Array) => string;
6
+ decode: (data: string) => Uint8Array<ArrayBuffer>;
7
+ };
8
+ export type ProfileSchemaType = BaseSchema<unknown, unknown, BaseIssue<unknown>> | BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>;
9
+ type ParentProfile = {
10
+ readonly schema: ZugferdProfile["schema"];
11
+ readonly build: ZugferdProfile["build"];
12
+ readonly rules?: ZugferdProfile["rules"];
13
+ };
14
+ type ParentProfileInput<P> = P extends {
15
+ readonly $Infer: {
16
+ readonly Input: infer Input;
17
+ };
18
+ } ? Input : P extends {
19
+ readonly schema: infer Schema extends ProfileSchemaType;
20
+ } ? InferInput<Schema> : never;
21
+ type ParentProfileOutput<P> = P extends {
22
+ readonly $Infer: {
23
+ readonly Output: infer Output;
24
+ };
25
+ } ? Output : P extends {
26
+ readonly schema: infer Schema extends ProfileSchemaType;
27
+ } ? InferOutput<Schema> : never;
28
+ type MergeProfileInputsIntersect<Use extends readonly ParentProfile[], LocalInput> = Use extends readonly [infer Head extends ParentProfile, ...infer Rest extends readonly ParentProfile[]] ? Rest extends readonly [] ? MergeProfileInput<ParentProfileInput<Head>, LocalInput> : MergeProfileInput<ParentProfileInput<Head>, MergeProfileInputsIntersect<Rest, LocalInput>> : LocalInput;
29
+ type MergeProfileOutputs<Use extends readonly ParentProfile[], LocalOutput> = Use extends readonly [infer Head extends ParentProfile, ...infer Rest extends readonly ParentProfile[]] ? Rest extends readonly [] ? ParentProfileOutput<Head> & LocalOutput : ParentProfileOutput<Head> & MergeProfileOutputs<Rest, LocalOutput> : LocalOutput;
30
+ type MergedProfileInput<Use extends readonly ParentProfile[], S extends ProfileSchemaType> = MergeProfileInputsIntersect<Use, InferInput<S>>;
31
+ type MergedProfileOutput<Use extends readonly ParentProfile[], S extends ProfileSchemaType> = MergeProfileOutputs<Use, InferOutput<S>>;
32
+ type ProfileData<Output> = Output & Record<string, any>;
33
+ export type CompactProfileSchema<Input, Output> = BaseSchema<Input, Output, BaseIssue<unknown>> | BaseSchemaAsync<Input, Output, BaseIssue<unknown>>;
34
+ export type DefinedProfile<Id extends string, DataRelationship extends ZugferdProfile["dataRelationship"], Input, Output, ExtensionSchema extends ZugferdProfile["extensionSchema"]> = {
35
+ readonly id: Id;
36
+ readonly dataRelationship: DataRelationship;
37
+ readonly use?: readonly ParentProfile[];
38
+ readonly build: (data: ProfileData<Output>, ctx: ZugferdProfileBuildContext) => void | Promise<void>;
39
+ readonly rules?: (data: ProfileData<Output>, ctx: ZugferdProfileRulesContext) => void | Promise<void>;
40
+ readonly schema: CompactProfileSchema<Input, Output>;
41
+ readonly extensionSchema: Omit<Required<MarkOptional<ExtensionSchema, "uri" | "fieldNameMap">>, "uri" | "namespace" | "fieldNameMap"> & {
42
+ uri?: string;
43
+ namespace?: string;
44
+ fieldNameMap?: Record<keyof ExtensionSchema, string>;
45
+ };
46
+ readonly $Infer: {
47
+ readonly Input: Input;
48
+ readonly Output: Output;
49
+ };
50
+ };
51
+ type ZugferdProfileInputBase<_Input, Output> = {
52
+ build: (data: ProfileData<Output>, ctx: ZugferdProfileBuildContext) => void | Promise<void>;
53
+ rules?: (data: ProfileData<Output>, ctx: ZugferdProfileRulesContext) => void | Promise<void>;
54
+ extensionSchema: MarkOptional<ZugferdProfile["extensionSchema"], "uri" | "namespace" | "fieldNameMap">;
55
+ };
56
+ export type ZugferdProfileInput<S extends ProfileSchemaType, Use extends readonly ParentProfile[] | undefined> = Omit<ZugferdProfile<S>, "build" | "rules" | "extensionSchema"> & ZugferdProfileInputBase<Use extends readonly ParentProfile[] ? MergedProfileOutput<Use, S> : InferOutput<S>, Use extends readonly ParentProfile[] ? MergedProfileOutput<Use, S> : InferOutput<S>> & {
57
+ use?: Use | undefined;
58
+ };
59
+ export declare function defineProfileSchema<const S extends ProfileSchemaType>(schema: S): CompactProfileSchema<InferInput<S>, InferOutput<S>>;
60
+ export declare function defineProfile<const Id extends string, const DataRelationship extends ZugferdProfile["dataRelationship"], const Schema extends ProfileSchemaType, const ExtensionSchema extends ZugferdProfile["extensionSchema"], const Input extends InferInput<Schema> = InferInput<Schema>, const Output extends InferOutput<Schema> = InferOutput<Schema>>(profile: ZugferdProfileInput<Schema, undefined> & {
61
+ id: Id;
62
+ dataRelationship: DataRelationship;
63
+ extensionSchema: ExtensionSchema;
64
+ schema: Schema;
65
+ use?: undefined;
66
+ }): DefinedProfile<Id, DataRelationship, Input, Output, ExtensionSchema>;
67
+ export declare function defineProfile<const Id extends string, const DataRelationship extends ZugferdProfile["dataRelationship"], const Schema extends ProfileSchemaType, const ExtensionSchema extends ZugferdProfile["extensionSchema"], const Use extends readonly ParentProfile[], const Input extends MergedProfileInput<Use, Schema> = MergedProfileInput<Use, Schema>, const Output extends MergedProfileOutput<Use, Schema> = MergedProfileOutput<Use, Schema>>(profile: ZugferdProfileInput<Schema, Use> & {
68
+ id: Id;
69
+ dataRelationship: DataRelationship;
70
+ extensionSchema: ExtensionSchema;
71
+ schema: Schema;
72
+ use: Use;
73
+ }): DefinedProfile<Id, DataRelationship, Input, Output, ExtensionSchema>;
74
+ export type { MergedProfileInput, MergedProfileOutput, XMLBuilder };
package/dist/index.mjs ADDED
@@ -0,0 +1,54 @@
1
+ import { intersect, intersectAsync } from "@node-zugferd/data-types";
2
+ // src/base64.ts
3
+ const base64 = {
4
+ encode: (data) => {
5
+ const bytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
6
+ let binary = "";
7
+ for (const byte of bytes) binary += String.fromCharCode(byte);
8
+ return btoa(binary);
9
+ },
10
+ decode: (data) => {
11
+ const binary = atob(data);
12
+ return Uint8Array.from(binary, (char) => char.charCodeAt(0));
13
+ }
14
+ };
15
+ // src/profile.ts
16
+ function schemaIsAsync(schema) {
17
+ if (schema.async) return true;
18
+ if (schema.type === "intersect" && "options" in schema) return schema.options.some(schemaIsAsync);
19
+ return false;
20
+ }
21
+ function defineProfileSchema(schema) {
22
+ return schema;
23
+ }
24
+ function defineProfile(profile) {
25
+ const { extensionSchema, use, schema, build, rules, ...input } = profile;
26
+ const mergedSchemas = use ? [...use.map((p) => p.schema), schema] : void 0;
27
+ const mergedSchema = mergedSchemas ? mergedSchemas.some(schemaIsAsync) ? intersectAsync(mergedSchemas) : intersect(mergedSchemas) : schema;
28
+ return {
29
+ ...input,
30
+ schema: mergedSchema,
31
+ extensionSchema: {
32
+ ...extensionSchema,
33
+ uri: extensionSchema.uri ?? "urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0#",
34
+ namespace: extensionSchema.namespace ?? "fx",
35
+ fieldNameMap: {
36
+ type: extensionSchema.fieldNameMap?.type ?? "DocumentType",
37
+ fileName: extensionSchema.fieldNameMap?.fileName ?? "DocumentFileName",
38
+ version: extensionSchema.fieldNameMap?.version ?? "Version",
39
+ conformanceLevel: extensionSchema.fieldNameMap?.conformanceLevel ?? "ConformanceLevel"
40
+ }
41
+ },
42
+ async build(data, ctx) {
43
+ const context = ctx;
44
+ if (use) for (const parent of use) await parent.build(data, context);
45
+ await build(data, context);
46
+ },
47
+ async rules(data, ctx) {
48
+ if (use) await Promise.all(use.map((parent) => parent.rules?.(data, ctx)));
49
+ await rules?.(data, ctx);
50
+ },
51
+ $Infer: {}
52
+ };
53
+ }
54
+ export { base64, defineProfile, defineProfileSchema };
package/package.json CHANGED
@@ -1,6 +1,35 @@
1
1
  {
2
2
  "name": "@node-zugferd/utils",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.0-beta.2",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "main": "dist/index.mjs",
7
+ "module": "dist/index.mjs",
8
+ "types": "dist/index.d.mts",
9
+ "dependencies": {
10
+ "@node-zugferd/core": "1.0.0-beta.2",
11
+ "@node-zugferd/data-types": "1.0.0-beta.2"
12
+ },
13
+ "devDependencies": {
14
+ "tsdown": "^0.23.5"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "dev-source": "./src/index.ts",
19
+ "types": "./dist/index.d.mts",
20
+ "default": "./dist/index.mjs"
21
+ }
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "scripts": {
27
+ "build": "tsdown",
28
+ "dev": "tsdown --watch",
29
+ "lint:package": "publint run --strict --pack false",
30
+ "lint:types": "attw --profile esm-only --pack .",
31
+ "typecheck": "tsc --project tsconfig.json",
32
+ "test": "vitest",
33
+ "coverage": "vitest run --coverage --coverage.provider=istanbul"
34
+ }
6
35
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.