@adobe/aio-commerce-lib-core 0.2.0 → 0.4.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.
@@ -1,21 +0,0 @@
1
- import { ErrorType } from "./result-BooxElIJ.js";
2
- import { GenericIssue } from "valibot";
3
-
4
- //#region source/lib/validation.d.ts
5
- declare namespace validation_d_exports {
6
- export { ValidationError, ValidationErrorType, makeValidationError, summarizeValidationError };
7
- }
8
- type ValidationErrorTag = `${string}ValidationError`;
9
- declare function summarizeValidationError(error: ValidationError): string;
10
- declare function makeValidationError<TIssues extends GenericIssue[] = GenericIssue[], TInfo extends Record<string, unknown> = Record<string, unknown>>(message: string, issues: TIssues, info?: TInfo): {
11
- _tag: "ValidationError";
12
- message: string;
13
- issues: TIssues;
14
- } & TInfo;
15
- type ValidationErrorType<TErrorTag extends ValidationErrorTag = "ValidationError", TIssues extends GenericIssue[] = GenericIssue[], TInfo extends Record<string, unknown> = Record<string, unknown>> = ErrorType<TErrorTag, TInfo & {
16
- message: string;
17
- issues: TIssues;
18
- }>;
19
- type ValidationError = ValidationErrorType<ValidationErrorTag, GenericIssue[], Record<string, unknown>>;
20
- //#endregion
21
- export { ValidationError, ValidationErrorType, makeValidationError, summarizeValidationError, validation_d_exports };
@@ -1,54 +0,0 @@
1
- import { __export } from "./chunk-Cl8Af3a2.js";
2
- import { cyanBright, dim, whiteBright, yellowBright } from "ansis";
3
- import { getDotPath } from "valibot";
4
-
5
- //#region source/lib/validation.ts
6
- var validation_exports = {};
7
- __export(validation_exports, {
8
- makeValidationError: () => makeValidationError,
9
- summarizeValidationError: () => summarizeValidationError
10
- });
11
- const LAST_RETURN_CHAR = "└── ";
12
- const RETURN_CHAR = "├── ";
13
- const ISSUE_KIND_TO_ERROR_TITLE = {
14
- schema: "Schema validation error",
15
- validation: "Input error",
16
- transformation: "Transformation error"
17
- };
18
- function issueToDisplay(issues) {
19
- const lines = issues.map((issue, index) => {
20
- const returnChar = cyanBright(index === issues.length - 1 ? LAST_RETURN_CHAR : RETURN_CHAR);
21
- const kindText = yellowBright(ISSUE_KIND_TO_ERROR_TITLE[issue.kind] || "Unknown issue kind");
22
- const dotPath = getDotPath(issue);
23
- const path = dotPath ? cyanBright(dotPath) + whiteBright(dim(" →")) : "";
24
- const issueLine = `${kindText} ${whiteBright("at")} ${path} ${whiteBright(issue.message)}`;
25
- return `${returnChar} ${issueLine}`;
26
- });
27
- return lines.join("\n");
28
- }
29
- /**
30
- * Summarizes a validation error by displaying the error message and the issues.
31
- * @param error The validation error to summarize.
32
- * @returns A pretty-printed string containing the summary of the validation error.
33
- */
34
- function summarizeValidationError(error) {
35
- return `${whiteBright(error.message)}\n${issueToDisplay(error.issues)}`;
36
- }
37
- /**
38
- * Creates a validation error.
39
- * @param message The validation error message.
40
- * @param issues The issues that occurred during validation.
41
- * @param info Additional information about the error.
42
- * @returns A validation error.
43
- */
44
- function makeValidationError(message, issues, info = {}) {
45
- return {
46
- _tag: "ValidationError",
47
- message,
48
- issues,
49
- ...info
50
- };
51
- }
52
-
53
- //#endregion
54
- export { makeValidationError, summarizeValidationError, validation_exports };
@@ -1,2 +0,0 @@
1
- import { Err, ErrorTag, ErrorType, Ok, Result, err, isErr, isOk, map, mapErr, ok, unwrap, unwrapErr } from "../result-B_qN_g1r.cjs";
2
- export { Err, ErrorTag, ErrorType, Ok, Result, err, isErr, isOk, map, mapErr, ok, unwrap, unwrapErr };
@@ -1,3 +0,0 @@
1
- import "../result-B_qN_g1r.cjs";
2
- import { ValidationError, ValidationErrorType, makeValidationError, summarizeValidationError } from "../validation-BonM9W9Z.cjs";
3
- export { ValidationError, ValidationErrorType, makeValidationError, summarizeValidationError };
package/source/index.ts DELETED
@@ -1,15 +0,0 @@
1
- /*
2
- * Copyright 2025 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- // Export here the public API of the package.
14
- export * as Result from "./lib/result";
15
- export * as Validation from "./lib/validation";
@@ -1,156 +0,0 @@
1
- /*
2
- * Copyright 2025 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- const SuccessOrFailure = {
14
- SUCCESS: "success",
15
- FAILURE: "failure",
16
- } as const;
17
-
18
- /** Defines the tag of an error type. */
19
- export type ErrorTag = `${string}Error`;
20
-
21
- /** Defines a successful result. */
22
- export type Ok<T> = { type: "success"; value: T };
23
-
24
- /** Defines a failed result. */
25
- export type Err<E extends ErrorType<ErrorTag>> = {
26
- type: "failure";
27
- error: E;
28
- };
29
-
30
- /** Defines a result that can be either a success or a failure. */
31
- export type Result<T, E extends ErrorType<ErrorTag>> = Ok<T> | Err<E>;
32
-
33
- /** Defines an error type that can contain any additional properties. */
34
- export type ErrorType<
35
- TTag extends ErrorTag = `Error`,
36
- TInfo extends Record<string, unknown> = Record<string, unknown>,
37
- > = TInfo & {
38
- _tag: TTag;
39
- };
40
-
41
- /**
42
- * Wraps the given value in a successful result.
43
- * @param value The value to wrap.
44
- * @returns A successful result containing the given value.
45
- */
46
- export function ok<T>(value: T): Ok<T> {
47
- return { type: SuccessOrFailure.SUCCESS, value };
48
- }
49
-
50
- /**
51
- * Wraps the given error in a failed result.
52
- * @param error The error to wrap.
53
- * @returns A failed result containing the given error.
54
- */
55
- export function err<E extends ErrorType<ErrorTag>>(error: E): Err<E> {
56
- return { type: SuccessOrFailure.FAILURE, error };
57
- }
58
-
59
- /**
60
- * Unwraps a result to retrieve its value.
61
- * If the result is a failure, an error will be thrown.
62
- *
63
- * @param result The result to unwrap.
64
- * @returns The value contained in the successful result.
65
- * @throws An error if the result is a failure.
66
- */
67
- export function unwrap<T, E extends ErrorType<ErrorTag>>(result: Result<T, E>) {
68
- if (result.type === SuccessOrFailure.SUCCESS) {
69
- return result.value satisfies T;
70
- }
71
-
72
- throw new Error(
73
- "Can't unwrap the value from this result because it's a failure",
74
- );
75
- }
76
-
77
- /**
78
- * Unwraps a result to retrieve its error.
79
- * If the result is a success, an error will be thrown.
80
- *
81
- * @param result The result to unwrap.
82
- * @returns The error contained in the failed result.
83
- * @throws An error if the result is a success.
84
- */
85
- export function unwrapErr<T, E extends ErrorType<ErrorTag>>(
86
- result: Result<T, E>,
87
- ) {
88
- if (result.type === SuccessOrFailure.FAILURE) {
89
- return result.error satisfies E;
90
- }
91
-
92
- throw new Error(
93
- "Can't unwrap the error from this result because it's a success",
94
- );
95
- }
96
-
97
- /**
98
- * Checks if a result is a success.
99
- * @param result The result to check.
100
- * @returns True if the result is a success, false otherwise.
101
- */
102
- export function isOk<T, E extends ErrorType<ErrorTag>>(
103
- result: Result<T, E>,
104
- ): result is Ok<T> {
105
- return result.type === SuccessOrFailure.SUCCESS;
106
- }
107
-
108
- /**
109
- * Checks if a result is a failure.
110
- * @param result The result to check.
111
- * @returns True if the result is a failure, false otherwise.
112
- */
113
- export function isErr<T, E extends ErrorType<ErrorTag>>(
114
- result: Result<T, E>,
115
- ): result is Err<E> {
116
- return result.type === SuccessOrFailure.FAILURE;
117
- }
118
-
119
- /**
120
- * Maps a successful result to another value.
121
- * If the result is a failure, it is returned unchanged.
122
- *
123
- * @param result The result to map.
124
- * @param fn A function to transform the success value.
125
- * @returns A new {@link Result} with the transformed value or the original error.
126
- */
127
- export function map<T, U, E extends ErrorType<ErrorTag>>(
128
- result: Result<T, E>,
129
- fn: (value: T) => U,
130
- ): Result<U, E> {
131
- if (isOk(result)) {
132
- return ok(fn(result.value));
133
- }
134
-
135
- return result;
136
- }
137
-
138
- /**
139
- * Maps a failed result's error to another error value.
140
- * If the result is a success, it is returned unchanged.
141
- *
142
- * @param result The result to map.
143
- * @param fn A function to transform the error value.
144
- * @returns A new Result with the original value or transformed error.
145
- */
146
- export function mapErr<
147
- T,
148
- E extends ErrorType<ErrorTag>,
149
- F extends ErrorType<ErrorTag>,
150
- >(result: Result<T, E>, fn: (error: E) => F): Result<T, F> {
151
- if (isErr(result)) {
152
- return err(fn(result.error));
153
- }
154
-
155
- return result;
156
- }
@@ -1,97 +0,0 @@
1
- /*
2
- * Copyright 2025 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import { cyanBright, dim, whiteBright, yellowBright } from "ansis";
14
- import { type BaseIssue, type GenericIssue, getDotPath } from "valibot";
15
-
16
- import type { ErrorType } from "./result";
17
-
18
- const LAST_RETURN_CHAR = "└── ";
19
- const RETURN_CHAR = "├── ";
20
-
21
- type IssueKind = "schema" | "validation" | "transformation";
22
- type ValidationErrorTag = `${string}ValidationError`;
23
-
24
- const ISSUE_KIND_TO_ERROR_TITLE: Record<IssueKind, string> = {
25
- schema: "Schema validation error",
26
- validation: "Input error",
27
- transformation: "Transformation error",
28
- };
29
-
30
- function issueToDisplay<TInput>(issues: BaseIssue<TInput>[]) {
31
- const lines = issues.map((issue, index) => {
32
- const returnChar = cyanBright(
33
- index === issues.length - 1 ? LAST_RETURN_CHAR : RETURN_CHAR,
34
- );
35
-
36
- const kindText = yellowBright(
37
- ISSUE_KIND_TO_ERROR_TITLE[issue.kind as IssueKind] ||
38
- "Unknown issue kind",
39
- );
40
-
41
- const dotPath = getDotPath(issue);
42
- const path = dotPath ? cyanBright(dotPath) + whiteBright(dim(" →")) : "";
43
- const issueLine = `${kindText} ${whiteBright("at")} ${path} ${whiteBright(issue.message)}`;
44
-
45
- return `${returnChar} ${issueLine}`;
46
- });
47
-
48
- return lines.join("\n");
49
- }
50
-
51
- /**
52
- * Summarizes a validation error by displaying the error message and the issues.
53
- * @param error The validation error to summarize.
54
- * @returns A pretty-printed string containing the summary of the validation error.
55
- */
56
- export function summarizeValidationError(error: ValidationError) {
57
- return `${whiteBright(error.message)}\n${issueToDisplay(error.issues)}`;
58
- }
59
-
60
- /**
61
- * Creates a validation error.
62
- * @param message The validation error message.
63
- * @param issues The issues that occurred during validation.
64
- * @param info Additional information about the error.
65
- * @returns A validation error.
66
- */
67
- export function makeValidationError<
68
- TIssues extends GenericIssue[] = GenericIssue[],
69
- TInfo extends Record<string, unknown> = Record<string, unknown>,
70
- >(message: string, issues: TIssues, info: TInfo = {} as TInfo) {
71
- return {
72
- _tag: "ValidationError",
73
- message,
74
- issues,
75
- ...info,
76
- } satisfies ValidationErrorType<ValidationErrorTag, TIssues, TInfo>;
77
- }
78
-
79
- /** Defines a generic type used to instantiate custom validation error interfaces. */
80
- export type ValidationErrorType<
81
- TErrorTag extends ValidationErrorTag = "ValidationError",
82
- TIssues extends GenericIssue[] = GenericIssue[],
83
- TInfo extends Record<string, unknown> = Record<string, unknown>,
84
- > = ErrorType<
85
- TErrorTag,
86
- TInfo & {
87
- message: string;
88
- issues: TIssues;
89
- }
90
- >;
91
-
92
- /** Defines a validation error. */
93
- export type ValidationError = ValidationErrorType<
94
- ValidationErrorTag,
95
- GenericIssue[],
96
- Record<string, unknown>
97
- >;
@@ -1,56 +0,0 @@
1
- /*
2
- * Copyright 2025 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import { describe, expect, test } from "vitest";
14
- import { err, isErr, isOk, ok, unwrap, unwrapErr } from "~/lib/result";
15
-
16
- describe("aio-commerce-lib-core/result", () => {
17
- test("should wrap a value in a success result", () => {
18
- const result = ok("success");
19
- expect(result).toEqual({ type: "success", value: "success" });
20
-
21
- const value = unwrap(result);
22
- expect(value).toBe("success");
23
- });
24
-
25
- test("should wrap an error in a failure result", () => {
26
- const result = err({ _tag: "Error", message: "error" });
27
- expect(result).toEqual({
28
- type: "failure",
29
- error: { _tag: "Error", message: "error" },
30
- });
31
-
32
- const error = unwrapErr(result);
33
- expect(error).toEqual({ _tag: "Error", message: "error" });
34
- });
35
-
36
- test("should unwrap a success result", () => {
37
- const result = ok("success");
38
- const value = unwrap(result);
39
- expect(value).toBe("success");
40
- });
41
-
42
- test("should unwrap a failure result", () => {
43
- const result = err({ _tag: "Error", message: "error" });
44
- expect(() => unwrap(result)).toThrow();
45
- });
46
-
47
- test("should check if a result is a success", () => {
48
- const result = ok("success");
49
- expect(isOk(result)).toBe(true);
50
- });
51
-
52
- test("should check if a result is a failure", () => {
53
- const result = err({ _tag: "Error", message: "error" });
54
- expect(isErr(result)).toBe(true);
55
- });
56
- });
@@ -1,71 +0,0 @@
1
- /*
2
- * Copyright 2025 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import { boolean, number, object, safeParse, string } from "valibot";
14
- import { describe, expect, test } from "vitest";
15
-
16
- import {
17
- makeValidationError,
18
- summarizeValidationError,
19
- } from "~/lib/validation";
20
-
21
- const SimpleObjectSchema = object({
22
- key1: string(),
23
- key2: number(),
24
- key3: object({
25
- nestedKey: object({
26
- nestedKey: boolean(),
27
- }),
28
- }),
29
- });
30
-
31
- const simpleObject = {
32
- key3: {
33
- nestedKey: {
34
- nestedKey: "nestedKey",
35
- },
36
- },
37
- };
38
-
39
- describe("aio-commerce-lib-core/validation", () => {
40
- test("should summarize a list of issues with colors and structure", () => {
41
- const result = safeParse(SimpleObjectSchema, simpleObject);
42
-
43
- if (!result.success) {
44
- // `result.issues` is only satisfied if the validation fails.
45
- const validationError = makeValidationError(
46
- "Validation failed",
47
- result.issues,
48
- );
49
-
50
- const output = summarizeValidationError(validationError);
51
- expect(output).toContain("key1");
52
- expect(output).toContain("key2");
53
- expect(output).toContain("key3.nestedKey.nestedKey");
54
- }
55
- });
56
-
57
- test("should make a validation error", () => {
58
- const result = safeParse(SimpleObjectSchema, simpleObject);
59
-
60
- if (!result.success) {
61
- const validationError = makeValidationError(
62
- "Validation failed",
63
- result.issues,
64
- );
65
-
66
- expect(validationError._tag).toBe("ValidationError");
67
- expect(validationError.message).toBe("Validation failed");
68
- expect(validationError.issues.length).toBeGreaterThan(0);
69
- }
70
- });
71
- });
package/tsconfig.json DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "extends": "@aio-commerce-sdk/config-typescript/tsconfig.base.json",
3
- "compilerOptions": {
4
- "baseUrl": ".",
5
- "rootDir": ".",
6
- "outDir": "./dist",
7
-
8
- "paths": {
9
- "~/*": ["./source/*"],
10
- "~~/*": ["./*"]
11
- }
12
- },
13
-
14
- "include": ["tsdown.config.ts", "source/**/*", "test/**/*"]
15
- }
package/tsdown.config.ts DELETED
@@ -1,11 +0,0 @@
1
- import { baseConfig } from "@aio-commerce-sdk/config-tsdown/tsdown.config.base";
2
- import { defineConfig } from "tsdown";
3
-
4
- export default defineConfig({
5
- ...baseConfig,
6
- entry: [
7
- "./source/index.ts",
8
- "./source/lib/result.ts",
9
- "./source/lib/validation.ts",
10
- ],
11
- });
package/vitest.config.ts DELETED
@@ -1,16 +0,0 @@
1
- import { resolve } from "node:path";
2
- import { defineConfig } from "vitest/config";
3
-
4
- export default defineConfig({
5
- resolve: {
6
- alias: {
7
- "~": resolve(__dirname, "./source"),
8
- "~~": resolve(__dirname, "./"),
9
- },
10
- },
11
-
12
- test: {
13
- globals: true,
14
- environment: "node",
15
- },
16
- });