@adobe/aio-commerce-lib-core 0.2.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.
- package/.turbo/turbo-build.log +36 -0
- package/CHANGELOG.md +12 -0
- package/LICENSE +201 -0
- package/README.md +154 -0
- package/dist/cjs/chunk-CsX-DzYB.cjs +42 -0
- package/dist/cjs/index.cjs +15 -0
- package/dist/cjs/index.d.cts +3 -0
- package/dist/cjs/lib/result.cjs +10 -0
- package/dist/cjs/lib/validation.cjs +4 -0
- package/dist/cjs/result-B_qN_g1r.d.cts +26 -0
- package/dist/cjs/result-fxmTDycS.cjs +160 -0
- package/dist/cjs/validation-12iMy71C.cjs +71 -0
- package/dist/cjs/validation-BonM9W9Z.d.cts +21 -0
- package/dist/es/chunk-Cl8Af3a2.js +11 -0
- package/dist/es/index.d.ts +3 -0
- package/dist/es/index.js +4 -0
- package/dist/es/lib/result.d.ts +2 -0
- package/dist/es/lib/result.js +3 -0
- package/dist/es/lib/validation.d.ts +3 -0
- package/dist/es/lib/validation.js +3 -0
- package/dist/es/result-B394X-_m.js +107 -0
- package/dist/es/result-BooxElIJ.d.ts +26 -0
- package/dist/es/validation-B2JQpDTl.d.ts +21 -0
- package/dist/es/validation-DmGRxtJ7.js +54 -0
- package/dist/lib/result.d.cts +2 -0
- package/dist/lib/validation.d.cts +3 -0
- package/package.json +77 -0
- package/source/index.ts +15 -0
- package/source/lib/result.ts +156 -0
- package/source/lib/validation.ts +97 -0
- package/test/result.test.ts +56 -0
- package/test/validation.test.ts +71 -0
- package/tsconfig.json +15 -0
- package/tsdown.config.ts +11 -0
- package/vitest.config.ts +16 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
|
|
2
|
+
> @adobe/aio-commerce-lib-core@0.2.0 build /home/runner/work/aio-commerce-sdk/aio-commerce-sdk/packages/aio-commerce-lib-core
|
|
3
|
+
> tsdown
|
|
4
|
+
|
|
5
|
+
[34mℹ[39m tsdown [2mv0.12.9[22m powered by rolldown [2mv1.0.0-beta.24[22m
|
|
6
|
+
[34mℹ[39m Using tsdown config: [4m/home/runner/work/aio-commerce-sdk/aio-commerce-sdk/packages/aio-commerce-lib-core/tsdown.config.ts[24m
|
|
7
|
+
[34mℹ[39m entry: [34msource/index.ts, source/lib/result.ts, source/lib/validation.ts[39m
|
|
8
|
+
[34mℹ[39m target: [34mnode22.0.0[39m
|
|
9
|
+
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
10
|
+
[34mℹ[39m Build start
|
|
11
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/cjs/[22m[1mindex.cjs[22m [2m0.40 kB[22m [2m│ gzip: 0.20 kB[22m
|
|
12
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/cjs/[22m[1mlib/result.cjs[22m [2m0.36 kB[22m [2m│ gzip: 0.14 kB[22m
|
|
13
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/cjs/[22m[1mlib/validation.cjs[22m [2m0.22 kB[22m [2m│ gzip: 0.12 kB[22m
|
|
14
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/cjs/[22mresult-fxmTDycS.cjs [2m3.88 kB[22m [2m│ gzip: 0.97 kB[22m
|
|
15
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/cjs/[22mvalidation-12iMy71C.cjs [2m2.39 kB[22m [2m│ gzip: 0.90 kB[22m
|
|
16
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/cjs/[22mchunk-CsX-DzYB.cjs [2m1.33 kB[22m [2m│ gzip: 0.59 kB[22m
|
|
17
|
+
[34mℹ[39m [33m[CJS][39m 6 files, total: 8.59 kB
|
|
18
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mlib/validation.d.cts[22m[39m [2m0.26 kB[22m [2m│ gzip: 0.13 kB[22m
|
|
19
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mlib/result.d.cts[22m[39m [2m0.23 kB[22m [2m│ gzip: 0.13 kB[22m
|
|
20
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mindex.d.cts[22m[39m [2m0.20 kB[22m [2m│ gzip: 0.12 kB[22m
|
|
21
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32mresult-B_qN_g1r.d.cts[39m [2m1.39 kB[22m [2m│ gzip: 0.41 kB[22m
|
|
22
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32mvalidation-BonM9W9Z.d.cts[39m [2m1.19 kB[22m [2m│ gzip: 0.40 kB[22m
|
|
23
|
+
[34mℹ[39m [33m[CJS][39m 5 files, total: 3.28 kB
|
|
24
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/es/[22m[1mindex.js[22m [2m0.19 kB[22m [2m│ gzip: 0.12 kB[22m
|
|
25
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/es/[22m[1mlib/result.js[22m [2m0.16 kB[22m [2m│ gzip: 0.10 kB[22m
|
|
26
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/es/[22m[1mlib/validation.js[22m [2m0.15 kB[22m [2m│ gzip: 0.10 kB[22m
|
|
27
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/es/[22mresult-B394X-_m.js [2m2.95 kB[22m [2m│ gzip: 0.86 kB[22m
|
|
28
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/es/[22mvalidation-DmGRxtJ7.js [2m1.92 kB[22m [2m│ gzip: 0.81 kB[22m
|
|
29
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/es/[22mchunk-Cl8Af3a2.js [2m0.23 kB[22m [2m│ gzip: 0.18 kB[22m
|
|
30
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/es/[22m[32m[1mlib/validation.d.ts[22m[39m [2m0.26 kB[22m [2m│ gzip: 0.13 kB[22m
|
|
31
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/es/[22m[32m[1mlib/result.d.ts[22m[39m [2m0.23 kB[22m [2m│ gzip: 0.12 kB[22m
|
|
32
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/es/[22m[32m[1mindex.d.ts[22m[39m [2m0.20 kB[22m [2m│ gzip: 0.12 kB[22m
|
|
33
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/es/[22m[32mresult-BooxElIJ.d.ts[39m [2m1.38 kB[22m [2m│ gzip: 0.41 kB[22m
|
|
34
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/es/[22m[32mvalidation-B2JQpDTl.d.ts[39m [2m1.18 kB[22m [2m│ gzip: 0.39 kB[22m
|
|
35
|
+
[34mℹ[39m [34m[ESM][39m 11 files, total: 8.85 kB
|
|
36
|
+
[32m✔[39m Build complete in [32m2032ms[39m
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# @adobe/aio-commerce-lib-core
|
|
2
|
+
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#18](https://github.com/adobe/aio-commerce-sdk/pull/18) [`aadbff1`](https://github.com/adobe/aio-commerce-sdk/commit/aadbff1acd08120f9d5cb8db4e3c849f552d8c79) Thanks [@jnatherley](https://github.com/jnatherley)! - Introduces the `aio-commerce-lib-core` package, which contains core utilities for the AIO Commerce SDK. It includes:
|
|
8
|
+
- A `Result` type based on Rust's `Result` type, to do better error handling.
|
|
9
|
+
- A set of validation utilities, including pretty printing of validation errors, and custom validation error types.
|
|
10
|
+
- Refactor aio-commerce-lib-auth to use aio-commerce-lib-core
|
|
11
|
+
|
|
12
|
+
Implements validation for the `aio-commerce-lib-auth` operations via `valibot`.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2025 Adobe
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# `@adobe/aio-commerce-lib-core`
|
|
2
|
+
|
|
3
|
+
> [!WARNING]
|
|
4
|
+
> This package is a work in progress and is not yet ready for use yet. You may be able to install it, but if you do, expect breaking changes.
|
|
5
|
+
|
|
6
|
+
This package provides core utilities for the Adobe Commerce SDK libraries.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```shell
|
|
11
|
+
pnpm install @adobe/aio-commerce-lib-core
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Overview
|
|
15
|
+
|
|
16
|
+
This package provides core utilities for the Adobe Commerce SDK libraries.
|
|
17
|
+
|
|
18
|
+
### Current Utilities
|
|
19
|
+
|
|
20
|
+
- **[`Result`](./source/lib/result.ts)**: A type-safe way to handle success and failure cases, without relying on exceptions. Inspired by the Rust [`Result` type](https://doc.rust-lang.org/std/result/).
|
|
21
|
+
- **[`Validation`](./source/lib/validation.ts)**: A set of utilities regarding validation, such as creating and pretty-printing validation errors.
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Result
|
|
26
|
+
|
|
27
|
+
Within every AppBuilder action you can use the `Result<T, E>` type to handle success and failure cases. The `Result` type is a union type that can either be a success with a value of type `T` or a failure with an error of type `E` that extends `ErrorType`.
|
|
28
|
+
|
|
29
|
+
#### Signatures
|
|
30
|
+
|
|
31
|
+
- `isOk(result: Result<T, E>): result is { ok: true, value: T }`
|
|
32
|
+
- `isErr(result: Result<T, E>): result is { ok: false, error: E }`
|
|
33
|
+
- `unwrap(result: Result<T, E>): T throws Error`
|
|
34
|
+
- `unwrapErr(result: Result<T, E>): E throws Error`
|
|
35
|
+
- `ok(value: T): Result<T, E>`
|
|
36
|
+
- `err(error: E): Result<T, E>`
|
|
37
|
+
|
|
38
|
+
#### Use case
|
|
39
|
+
|
|
40
|
+
- `isOk` and `isErr` are type guards that let you branch logic based on the result.
|
|
41
|
+
- `unwrap` returns the value if the result is a success, otherwise it throws.
|
|
42
|
+
- `unwrapErr` returns the error if the result is a failure, otherwise it throws.
|
|
43
|
+
- `ok` and `err` are helpers to create success and failure results.
|
|
44
|
+
|
|
45
|
+
<hr/>
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import {
|
|
49
|
+
type Result,
|
|
50
|
+
err,
|
|
51
|
+
isOk,
|
|
52
|
+
ok,
|
|
53
|
+
unwrap,
|
|
54
|
+
} from "@adobe/aio-commerce-lib-core/result";
|
|
55
|
+
|
|
56
|
+
// Simulated function returning a Result
|
|
57
|
+
const fetchProduct = async (id: string): Result<Product, Error> => {
|
|
58
|
+
const response = await api.getProductById(id);
|
|
59
|
+
if (response.statusCode === 200) {
|
|
60
|
+
return ok(response.data);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return err({
|
|
64
|
+
_tag: "ApiError",
|
|
65
|
+
error: new Error(response.error),
|
|
66
|
+
statusCode: response.statusCode,
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export const main = async function (params: Record<string, unknown>) {
|
|
71
|
+
const result = fetchProduct("valid");
|
|
72
|
+
|
|
73
|
+
// remaining business logic
|
|
74
|
+
};
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Result - isOk
|
|
78
|
+
|
|
79
|
+
Here you can use the isErr TypeGuard to check if the result is a failure, then handle the error by returning an appropriate response. Result is now typesafe, and you can access the value properties directly.
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
export const main = async function (params: Record<string, unknown>) {
|
|
83
|
+
// ... [PREVIOUS CODE]
|
|
84
|
+
// Use a the isOk TypeGuard to check if the result is a success
|
|
85
|
+
if (isOk(result)) {
|
|
86
|
+
// typesafe access to the value properties
|
|
87
|
+
const product = result.value;
|
|
88
|
+
// product: { id: 'valid', name: 'Sample Product' }
|
|
89
|
+
return {
|
|
90
|
+
status: 200,
|
|
91
|
+
body: product,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Result - isErr
|
|
98
|
+
|
|
99
|
+
In another example you can use the isErr TypeGuard to check if the result is a failure, then handle the error by returning an appropriate response. Result is now typesafe, and you can access the error properties directly.
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
export const main = async function (params: Record<string, unknown>) {
|
|
103
|
+
// Further down within the same action you can
|
|
104
|
+
// use a the isOk TypeGuard to check if the result is a success
|
|
105
|
+
if (isErr(result)) {
|
|
106
|
+
// typesafe access to the error properties
|
|
107
|
+
const error = result.error;
|
|
108
|
+
// error: {
|
|
109
|
+
// _tag: "ApiError",
|
|
110
|
+
// error: new Error(response.error),
|
|
111
|
+
// statusCode: response.statusCode
|
|
112
|
+
// }
|
|
113
|
+
return {
|
|
114
|
+
status: error.statusCode,
|
|
115
|
+
body: {
|
|
116
|
+
error: `CommerceApiError: ${error.error.message}`,
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Validation
|
|
124
|
+
|
|
125
|
+
The summarize function helps you pretty-print validation errors for easier debugging.
|
|
126
|
+
|
|
127
|
+
#### Signature
|
|
128
|
+
|
|
129
|
+
- `summarizeValidationError(error: ValidationError): string` - Pretty-prints a validation error.
|
|
130
|
+
- `makeValidationError(message: string, issues: [GenericIssue, ...GenericIssue[]]): ValidationError` - Creates a validation error.
|
|
131
|
+
|
|
132
|
+
#### Use case
|
|
133
|
+
|
|
134
|
+
This prints a readable summary of all validation issues from unknown input data.
|
|
135
|
+
|
|
136
|
+
<hr/>
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
import { summarize } from "@adobe/aio-commerce-lib-core/validation";
|
|
140
|
+
|
|
141
|
+
const validationError = makeValidationError("Invalid input", [...issues]);
|
|
142
|
+
console.error(summarizeValidationError(validationError));
|
|
143
|
+
|
|
144
|
+
// Output:
|
|
145
|
+
// Invalid input
|
|
146
|
+
// ├── Schema validation error at AIO_COMMERCE_INTEGRATIONS_CONSUMER_KEY → Missing or invalid commerce integration parameter "AIO_COMMERCE_INTEGRATIONS_CONSUMER_KEY"
|
|
147
|
+
// ├── Schema validation error at AIO_COMMERCE_INTEGRATIONS_CONSUMER_SECRET → Missing or invalid commerce integration parameter "AIO_COMMERCE_INTEGRATIONS_CONSUMER_SECRET"
|
|
148
|
+
// ├── Schema validation error at AIO_COMMERCE_INTEGRATIONS_ACCESS_TOKEN → Missing or invalid commerce integration parameter "AIO_COMMERCE_INTEGRATIONS_ACCESS_TOKEN"
|
|
149
|
+
// └── Schema validation error at AIO_COMMERCE_INTEGRATIONS_ACCESS_TOKEN_SECRET → Missing or invalid commerce integration parameter "AIO_COMMERCE_INTEGRATIONS_ACCESS_TOKEN_SECRET"
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Contributing
|
|
153
|
+
|
|
154
|
+
This package is part of the Adobe Commerce SDK monorepo. See the [Contributing Guide](https://github.com/adobe/aio-commerce-sdk/blob/main/.github/CONTRIBUTING.md) and [Development Guide](https://github.com/adobe/aio-commerce-sdk/blob/main/.github/DEVELOPMENT.md) for development setup and guidelines.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all) __defProp(target, name, {
|
|
10
|
+
get: all[name],
|
|
11
|
+
enumerable: true
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
var __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
16
|
+
key = keys[i];
|
|
17
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
18
|
+
get: ((k) => from[k]).bind(null, key),
|
|
19
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
25
|
+
value: mod,
|
|
26
|
+
enumerable: true
|
|
27
|
+
}) : target, mod));
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
|
|
31
|
+
Object.defineProperty(exports, '__export', {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
get: function () {
|
|
34
|
+
return __export;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
Object.defineProperty(exports, '__toESM', {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
get: function () {
|
|
40
|
+
return __toESM;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const require_result = require('./result-fxmTDycS.cjs');
|
|
2
|
+
const require_validation = require('./validation-12iMy71C.cjs');
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, 'Result', {
|
|
5
|
+
enumerable: true,
|
|
6
|
+
get: function () {
|
|
7
|
+
return require_result.result_exports;
|
|
8
|
+
}
|
|
9
|
+
});
|
|
10
|
+
Object.defineProperty(exports, 'Validation', {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return require_validation.validation_exports;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const require_result = require('../result-fxmTDycS.cjs');
|
|
2
|
+
|
|
3
|
+
exports.err = require_result.err;
|
|
4
|
+
exports.isErr = require_result.isErr;
|
|
5
|
+
exports.isOk = require_result.isOk;
|
|
6
|
+
exports.map = require_result.map;
|
|
7
|
+
exports.mapErr = require_result.mapErr;
|
|
8
|
+
exports.ok = require_result.ok;
|
|
9
|
+
exports.unwrap = require_result.unwrap;
|
|
10
|
+
exports.unwrapErr = require_result.unwrapErr;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
declare namespace result_d_exports {
|
|
2
|
+
export { Err, ErrorTag, ErrorType, Ok, Result, err, isErr, isOk, map, mapErr, ok, unwrap, unwrapErr };
|
|
3
|
+
}
|
|
4
|
+
type ErrorTag = `${string}Error`;
|
|
5
|
+
type Ok<T> = {
|
|
6
|
+
type: "success";
|
|
7
|
+
value: T;
|
|
8
|
+
};
|
|
9
|
+
type Err<E extends ErrorType<ErrorTag>> = {
|
|
10
|
+
type: "failure";
|
|
11
|
+
error: E;
|
|
12
|
+
};
|
|
13
|
+
type Result<T, E extends ErrorType<ErrorTag>> = Ok<T> | Err<E>;
|
|
14
|
+
type ErrorType<TTag extends ErrorTag = `Error`, TInfo extends Record<string, unknown> = Record<string, unknown>> = TInfo & {
|
|
15
|
+
_tag: TTag;
|
|
16
|
+
};
|
|
17
|
+
declare function ok<T>(value: T): Ok<T>;
|
|
18
|
+
declare function err<E extends ErrorType<ErrorTag>>(error: E): Err<E>;
|
|
19
|
+
declare function unwrap<T, E extends ErrorType<ErrorTag>>(result: Result<T, E>): T;
|
|
20
|
+
declare function unwrapErr<T, E extends ErrorType<ErrorTag>>(result: Result<T, E>): E;
|
|
21
|
+
declare function isOk<T, E extends ErrorType<ErrorTag>>(result: Result<T, E>): result is Ok<T>;
|
|
22
|
+
declare function isErr<T, E extends ErrorType<ErrorTag>>(result: Result<T, E>): result is Err<E>;
|
|
23
|
+
declare function map<T, U, E extends ErrorType<ErrorTag>>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;
|
|
24
|
+
declare function mapErr<T, E extends ErrorType<ErrorTag>, F extends ErrorType<ErrorTag>>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { Err, ErrorTag, ErrorType, Ok, Result, __export, err, isErr, isOk, map, mapErr, ok, result_d_exports, unwrap, unwrapErr };
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CsX-DzYB.cjs');
|
|
2
|
+
|
|
3
|
+
//#region source/lib/result.ts
|
|
4
|
+
var result_exports = {};
|
|
5
|
+
require_chunk.__export(result_exports, {
|
|
6
|
+
err: () => err,
|
|
7
|
+
isErr: () => isErr,
|
|
8
|
+
isOk: () => isOk,
|
|
9
|
+
map: () => map,
|
|
10
|
+
mapErr: () => mapErr,
|
|
11
|
+
ok: () => ok,
|
|
12
|
+
unwrap: () => unwrap,
|
|
13
|
+
unwrapErr: () => unwrapErr
|
|
14
|
+
});
|
|
15
|
+
const SuccessOrFailure = {
|
|
16
|
+
SUCCESS: "success",
|
|
17
|
+
FAILURE: "failure"
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Wraps the given value in a successful result.
|
|
21
|
+
* @param value The value to wrap.
|
|
22
|
+
* @returns A successful result containing the given value.
|
|
23
|
+
*/
|
|
24
|
+
function ok(value) {
|
|
25
|
+
return {
|
|
26
|
+
type: SuccessOrFailure.SUCCESS,
|
|
27
|
+
value
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Wraps the given error in a failed result.
|
|
32
|
+
* @param error The error to wrap.
|
|
33
|
+
* @returns A failed result containing the given error.
|
|
34
|
+
*/
|
|
35
|
+
function err(error) {
|
|
36
|
+
return {
|
|
37
|
+
type: SuccessOrFailure.FAILURE,
|
|
38
|
+
error
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Unwraps a result to retrieve its value.
|
|
43
|
+
* If the result is a failure, an error will be thrown.
|
|
44
|
+
*
|
|
45
|
+
* @param result The result to unwrap.
|
|
46
|
+
* @returns The value contained in the successful result.
|
|
47
|
+
* @throws An error if the result is a failure.
|
|
48
|
+
*/
|
|
49
|
+
function unwrap(result) {
|
|
50
|
+
if (result.type === SuccessOrFailure.SUCCESS) return result.value;
|
|
51
|
+
throw new Error("Can't unwrap the value from this result because it's a failure");
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Unwraps a result to retrieve its error.
|
|
55
|
+
* If the result is a success, an error will be thrown.
|
|
56
|
+
*
|
|
57
|
+
* @param result The result to unwrap.
|
|
58
|
+
* @returns The error contained in the failed result.
|
|
59
|
+
* @throws An error if the result is a success.
|
|
60
|
+
*/
|
|
61
|
+
function unwrapErr(result) {
|
|
62
|
+
if (result.type === SuccessOrFailure.FAILURE) return result.error;
|
|
63
|
+
throw new Error("Can't unwrap the error from this result because it's a success");
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Checks if a result is a success.
|
|
67
|
+
* @param result The result to check.
|
|
68
|
+
* @returns True if the result is a success, false otherwise.
|
|
69
|
+
*/
|
|
70
|
+
function isOk(result) {
|
|
71
|
+
return result.type === SuccessOrFailure.SUCCESS;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Checks if a result is a failure.
|
|
75
|
+
* @param result The result to check.
|
|
76
|
+
* @returns True if the result is a failure, false otherwise.
|
|
77
|
+
*/
|
|
78
|
+
function isErr(result) {
|
|
79
|
+
return result.type === SuccessOrFailure.FAILURE;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Maps a successful result to another value.
|
|
83
|
+
* If the result is a failure, it is returned unchanged.
|
|
84
|
+
*
|
|
85
|
+
* @param result The result to map.
|
|
86
|
+
* @param fn A function to transform the success value.
|
|
87
|
+
* @returns A new {@link Result} with the transformed value or the original error.
|
|
88
|
+
*/
|
|
89
|
+
function map(result, fn) {
|
|
90
|
+
if (isOk(result)) return ok(fn(result.value));
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Maps a failed result's error to another error value.
|
|
95
|
+
* If the result is a success, it is returned unchanged.
|
|
96
|
+
*
|
|
97
|
+
* @param result The result to map.
|
|
98
|
+
* @param fn A function to transform the error value.
|
|
99
|
+
* @returns A new Result with the original value or transformed error.
|
|
100
|
+
*/
|
|
101
|
+
function mapErr(result, fn) {
|
|
102
|
+
if (isErr(result)) return err(fn(result.error));
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
//#endregion
|
|
107
|
+
Object.defineProperty(exports, 'err', {
|
|
108
|
+
enumerable: true,
|
|
109
|
+
get: function () {
|
|
110
|
+
return err;
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
Object.defineProperty(exports, 'isErr', {
|
|
114
|
+
enumerable: true,
|
|
115
|
+
get: function () {
|
|
116
|
+
return isErr;
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
Object.defineProperty(exports, 'isOk', {
|
|
120
|
+
enumerable: true,
|
|
121
|
+
get: function () {
|
|
122
|
+
return isOk;
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
Object.defineProperty(exports, 'map', {
|
|
126
|
+
enumerable: true,
|
|
127
|
+
get: function () {
|
|
128
|
+
return map;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
Object.defineProperty(exports, 'mapErr', {
|
|
132
|
+
enumerable: true,
|
|
133
|
+
get: function () {
|
|
134
|
+
return mapErr;
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
Object.defineProperty(exports, 'ok', {
|
|
138
|
+
enumerable: true,
|
|
139
|
+
get: function () {
|
|
140
|
+
return ok;
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
Object.defineProperty(exports, 'result_exports', {
|
|
144
|
+
enumerable: true,
|
|
145
|
+
get: function () {
|
|
146
|
+
return result_exports;
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
Object.defineProperty(exports, 'unwrap', {
|
|
150
|
+
enumerable: true,
|
|
151
|
+
get: function () {
|
|
152
|
+
return unwrap;
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
Object.defineProperty(exports, 'unwrapErr', {
|
|
156
|
+
enumerable: true,
|
|
157
|
+
get: function () {
|
|
158
|
+
return unwrapErr;
|
|
159
|
+
}
|
|
160
|
+
});
|