@nexical/sdk-core 0.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.
- package/.github/workflows/deploy.yml +31 -0
- package/.husky/pre-commit +1 -0
- package/.prettierignore +8 -0
- package/.prettierrc +7 -0
- package/LICENSE +201 -0
- package/README.md +44 -0
- package/eslint.config.mjs +67 -0
- package/package.json +43 -0
- package/src/client.ts +81 -0
- package/src/core.ts +2 -0
- package/src/index.ts +9 -0
- package/src/resource.ts +35 -0
- package/tests/unit/README.md +51 -0
- package/tests/unit/client.test.ts +182 -0
- package/tests/unit/index.test.ts +12 -0
- package/tests/unit/resource.test.ts +92 -0
- package/tsconfig.json +14 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
name: Deploy Nexical SDK Core
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
deploy:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
name: Build & Publish
|
|
12
|
+
steps:
|
|
13
|
+
- name: Checkout Repo
|
|
14
|
+
uses: actions/checkout@v4
|
|
15
|
+
|
|
16
|
+
- name: Setup Node.js
|
|
17
|
+
uses: actions/setup-node@v4
|
|
18
|
+
with:
|
|
19
|
+
node-version: 22
|
|
20
|
+
registry-url: 'https://registry.npmjs.org'
|
|
21
|
+
|
|
22
|
+
- name: Install Dependencies
|
|
23
|
+
run: npm install
|
|
24
|
+
|
|
25
|
+
- name: Lint
|
|
26
|
+
run: npm run lint
|
|
27
|
+
|
|
28
|
+
- name: Publish to NPM
|
|
29
|
+
run: npm publish --access public
|
|
30
|
+
env:
|
|
31
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
npx lint-staged
|
package/.prettierignore
ADDED
package/.prettierrc
ADDED
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 2026 Fractal Synapse Labs LCA PBC
|
|
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,44 @@
|
|
|
1
|
+
# @nexical/sdk
|
|
2
|
+
|
|
3
|
+
This is the auto-generated Federated SDK for ArcNexus. It provides a unified client for accessing all module APIs.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
### Frontend
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
import { api } from '@/lib/api';
|
|
11
|
+
|
|
12
|
+
// Access module SDKs
|
|
13
|
+
const users = await api.user.list();
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### Adding a New Module
|
|
17
|
+
|
|
18
|
+
To expose your module's API through the SDK, follow these steps:
|
|
19
|
+
|
|
20
|
+
1. **Create SDK Entry Point**: Create `src/sdk/index.ts` inside your module (e.g., `modules/my-module/src/sdk/index.ts`).
|
|
21
|
+
2. **Inherit BaseResource**: Export a class that extends `BaseResource` from `@nexical/sdk-core`.
|
|
22
|
+
3. **Naming Convention**: The class **MUST** be named `{ModuleName}SDK` (PascalCase). For example, if your module is `modules/team`, the class must be `TeamSDK`.
|
|
23
|
+
4. **Auto-Discovery**: Run `npm run gen:sdk` (or restart the dev server) to auto-discover your new SDK module.
|
|
24
|
+
|
|
25
|
+
### Example
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
// modules/team/src/sdk/index.ts
|
|
29
|
+
import { BaseResource } from '@nexical/sdk';
|
|
30
|
+
|
|
31
|
+
export class TeamSDK extends BaseResource {
|
|
32
|
+
async list() {
|
|
33
|
+
return this.request('GET', '/teams');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async create(data: { name: string }) {
|
|
37
|
+
return this.request('POST', '/teams', data);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Type Safety
|
|
43
|
+
|
|
44
|
+
Import DTOs directly from your module's source (e.g., `../types`) to ensure strong typing in your SDK methods.
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import eslintPluginAstro from 'eslint-plugin-astro';
|
|
2
|
+
import eslintPluginReact from 'eslint-plugin-react';
|
|
3
|
+
import eslintPluginReactHooks from 'eslint-plugin-react-hooks';
|
|
4
|
+
import eslintPluginJsxA11y from 'eslint-plugin-jsx-a11y';
|
|
5
|
+
import eslintConfigPrettier from 'eslint-config-prettier';
|
|
6
|
+
import tseslint from 'typescript-eslint';
|
|
7
|
+
import globals from 'globals';
|
|
8
|
+
import js from '@eslint/js';
|
|
9
|
+
|
|
10
|
+
export default tseslint.config(
|
|
11
|
+
// Global ignore
|
|
12
|
+
{
|
|
13
|
+
ignores: ['**/dist/**', '**/node_modules/**', '**/coverage/**', '**/.astro/**', '**/.agent/**'],
|
|
14
|
+
},
|
|
15
|
+
|
|
16
|
+
// Base JS/TS configuration
|
|
17
|
+
js.configs.recommended,
|
|
18
|
+
...tseslint.configs.recommended,
|
|
19
|
+
|
|
20
|
+
// React configuration
|
|
21
|
+
{
|
|
22
|
+
files: ['**/*.{js,jsx,mjs,cjs,ts,tsx}'],
|
|
23
|
+
plugins: {
|
|
24
|
+
react: eslintPluginReact,
|
|
25
|
+
'react-hooks': eslintPluginReactHooks,
|
|
26
|
+
'jsx-a11y': eslintPluginJsxA11y,
|
|
27
|
+
},
|
|
28
|
+
languageOptions: {
|
|
29
|
+
parserOptions: {
|
|
30
|
+
ecmaFeatures: {
|
|
31
|
+
jsx: true,
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
globals: {
|
|
35
|
+
...globals.browser,
|
|
36
|
+
...globals.node,
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
settings: {
|
|
40
|
+
react: {
|
|
41
|
+
version: 'detect',
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
rules: {
|
|
45
|
+
...eslintPluginReact.configs.recommended.rules,
|
|
46
|
+
...eslintPluginReactHooks.configs.recommended.rules,
|
|
47
|
+
...eslintPluginJsxA11y.configs.recommended.rules,
|
|
48
|
+
'react/react-in-jsx-scope': 'off', // Not needed in React 17+ or Astro
|
|
49
|
+
'react/no-unknown-property': 'off', // Conflicts with some Astro/Web Component patterns
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
// Astro configuration
|
|
54
|
+
...eslintPluginAstro.configs.recommended,
|
|
55
|
+
|
|
56
|
+
// Custom rules override
|
|
57
|
+
{
|
|
58
|
+
rules: {
|
|
59
|
+
'@typescript-eslint/no-unused-vars': ['error', { args: 'none', varsIgnorePattern: '^_' }],
|
|
60
|
+
'@typescript-eslint/no-explicit-any': 'warn', // Downgrade to warning for now as codebase has some anys
|
|
61
|
+
'no-console': ['warn', { allow: ['warn', 'error', 'info'] }],
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
// Prettier must be last to override conflicting rules
|
|
66
|
+
eslintConfigPrettier,
|
|
67
|
+
);
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nexical/sdk-core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.ts",
|
|
9
|
+
"./core": "./src/core.ts"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"format": "prettier --write .",
|
|
13
|
+
"lint": "eslint .",
|
|
14
|
+
"lint:fix": "eslint . --fix",
|
|
15
|
+
"prepare": "husky"
|
|
16
|
+
},
|
|
17
|
+
"lint-staged": {
|
|
18
|
+
"**/*": [
|
|
19
|
+
"prettier --write --ignore-unknown"
|
|
20
|
+
],
|
|
21
|
+
"**/*.{js,jsx,ts,tsx,astro}": [
|
|
22
|
+
"eslint --fix"
|
|
23
|
+
]
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"glob": "^13.0.5"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@eslint/js": "^9.39.2",
|
|
30
|
+
"eslint": "^9.39.2",
|
|
31
|
+
"eslint-config-prettier": "^10.1.8",
|
|
32
|
+
"eslint-plugin-astro": "^1.6.0",
|
|
33
|
+
"eslint-plugin-jsx-a11y": "^6.10.2",
|
|
34
|
+
"eslint-plugin-react": "^7.37.5",
|
|
35
|
+
"eslint-plugin-react-hooks": "^7.0.1",
|
|
36
|
+
"globals": "^17.3.0",
|
|
37
|
+
"husky": "^9.1.7",
|
|
38
|
+
"lint-staged": "^16.2.7",
|
|
39
|
+
"prettier": "^3.8.1",
|
|
40
|
+
"typescript": "^5.9.3",
|
|
41
|
+
"typescript-eslint": "^8.56.0"
|
|
42
|
+
}
|
|
43
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
export interface AuthStrategy {
|
|
3
|
+
getHeaders(): Promise<Record<string, string>>;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface ApiClientOptions {
|
|
7
|
+
baseUrl: string;
|
|
8
|
+
authStrategy?: AuthStrategy;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class NexicalError extends Error {
|
|
12
|
+
constructor(
|
|
13
|
+
public status: number,
|
|
14
|
+
public statusText: string,
|
|
15
|
+
public data?: any,
|
|
16
|
+
) {
|
|
17
|
+
const message = data?.error || data?.message || statusText;
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = 'NexicalError';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class ApiClient {
|
|
24
|
+
private baseUrl: string;
|
|
25
|
+
private authStrategy?: AuthStrategy;
|
|
26
|
+
|
|
27
|
+
constructor(options: ApiClientOptions) {
|
|
28
|
+
this.baseUrl = options.baseUrl.replace(/\/$/, '');
|
|
29
|
+
this.authStrategy = options.authStrategy;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async request<T>(
|
|
33
|
+
method: string,
|
|
34
|
+
path: string,
|
|
35
|
+
body?: any,
|
|
36
|
+
options: RequestInit = {},
|
|
37
|
+
): Promise<T> {
|
|
38
|
+
const headers: Record<string, string> = {
|
|
39
|
+
'Content-Type': 'application/json',
|
|
40
|
+
...(options.headers as Record<string, string>),
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
if (this.authStrategy) {
|
|
44
|
+
const authHeaders = await this.authStrategy.getHeaders();
|
|
45
|
+
Object.assign(headers, authHeaders);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const config: RequestInit = {
|
|
49
|
+
method,
|
|
50
|
+
...options,
|
|
51
|
+
headers,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
if (!config.credentials) {
|
|
55
|
+
config.credentials = 'include';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (body) {
|
|
59
|
+
config.body = JSON.stringify(body);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const response = await fetch(`${this.baseUrl}${path}`, config);
|
|
63
|
+
|
|
64
|
+
if (!response.ok) {
|
|
65
|
+
let errorData;
|
|
66
|
+
const text = await response.text();
|
|
67
|
+
try {
|
|
68
|
+
errorData = JSON.parse(text);
|
|
69
|
+
} catch (e) {
|
|
70
|
+
errorData = text;
|
|
71
|
+
}
|
|
72
|
+
throw new NexicalError(response.status, response.statusText, errorData);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (response.status === 204) {
|
|
76
|
+
return {} as T;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return response.json();
|
|
80
|
+
}
|
|
81
|
+
}
|
package/src/core.ts
ADDED
package/src/index.ts
ADDED
package/src/resource.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ApiClient } from './client.js';
|
|
2
|
+
|
|
3
|
+
export abstract class BaseResource {
|
|
4
|
+
constructor(protected client: ApiClient) {}
|
|
5
|
+
|
|
6
|
+
protected _request<T>(
|
|
7
|
+
method: string,
|
|
8
|
+
path: string,
|
|
9
|
+
body?: unknown,
|
|
10
|
+
options?: RequestInit,
|
|
11
|
+
): Promise<T> {
|
|
12
|
+
return this.client.request<T>(method, path, body, options);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
protected buildQuery(filters: Record<string, unknown> = {}): string {
|
|
16
|
+
const params = new URLSearchParams();
|
|
17
|
+
|
|
18
|
+
const process = (prefix: string, obj: Record<string, unknown>) => {
|
|
19
|
+
for (const key in obj) {
|
|
20
|
+
const value = obj[key];
|
|
21
|
+
const fullKey = prefix ? `${prefix}__${key}` : key;
|
|
22
|
+
|
|
23
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
24
|
+
process(fullKey, value as Record<string, unknown>);
|
|
25
|
+
} else if (value !== undefined && value !== null) {
|
|
26
|
+
params.append(fullKey, String(value));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
process('', filters);
|
|
32
|
+
const qs = params.toString();
|
|
33
|
+
return qs ? `?${qs}` : '';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Nexical Core Unit Testing
|
|
2
|
+
|
|
3
|
+
This directory contains unit tests for the Nexical Ecosystem core located in `src/`.
|
|
4
|
+
|
|
5
|
+
## Philosophy
|
|
6
|
+
|
|
7
|
+
Unit tests in Nexical should:
|
|
8
|
+
|
|
9
|
+
- Be **Fast**: They should execute in milliseconds.
|
|
10
|
+
- Be **Isolated**: No network calls, database interaction, or file system access (unless using a memory-only provider).
|
|
11
|
+
- Be **Predictable**: Given the same input, they always produce the same output.
|
|
12
|
+
- Target **Business Logic**: Focus on utility functions, state management, and core services.
|
|
13
|
+
|
|
14
|
+
> [!IMPORTANT]
|
|
15
|
+
> **CRITICAL RULE**: Unit tests MUST follow the exact same folder structure as the `src/` files being tested.
|
|
16
|
+
> For example:
|
|
17
|
+
>
|
|
18
|
+
> - `src/lib/core/config.ts` -> `tests/unit/lib/core/config.test.ts`
|
|
19
|
+
> - `src/middleware.ts` -> `tests/unit/middleware.test.ts`
|
|
20
|
+
|
|
21
|
+
## Running Tests
|
|
22
|
+
|
|
23
|
+
To run all unit tests:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm run test:unit
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
To run tests with coverage:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npx vitest run -c vitest.unit.config.ts --coverage
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Directory Structure
|
|
36
|
+
|
|
37
|
+
- `tests/unit/core`: Tests for core system logic.
|
|
38
|
+
- `tests/unit/lib`: Tests for utility functions in `src/lib`.
|
|
39
|
+
- `tests/unit/hooks`: Tests for React/Astro hooks.
|
|
40
|
+
- `tests/unit/helpers.ts`: Shared testing utilities and mocks.
|
|
41
|
+
|
|
42
|
+
## Mocking Strategy
|
|
43
|
+
|
|
44
|
+
We use `vitest`'s built-in mocking capabilities. For core services:
|
|
45
|
+
|
|
46
|
+
- Mock dependencies using `vi.mock()`.
|
|
47
|
+
- Use the helpers in `tests/unit/helpers.ts` for common patterns.
|
|
48
|
+
|
|
49
|
+
## Coverage Requirements
|
|
50
|
+
|
|
51
|
+
We aim for **>80%** line and function coverage for the core library. Coverage reports are generated in the `coverage/` directory.
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { ApiClient, NexicalError } from '../../src/client.ts';
|
|
3
|
+
|
|
4
|
+
describe('ApiClient', () => {
|
|
5
|
+
const baseUrl = 'https://api.example.com/';
|
|
6
|
+
let client: ApiClient;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
vi.stubGlobal('fetch', vi.fn());
|
|
10
|
+
client = new ApiClient({ baseUrl });
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should strip trailing slash from baseUrl', () => {
|
|
14
|
+
const clientWithSlash = new ApiClient({ baseUrl: 'https://example.com/' });
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
+
expect((clientWithSlash as any).baseUrl).toBe('https://example.com');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should make a successful request', async () => {
|
|
20
|
+
const mockResponse = { data: 'test' };
|
|
21
|
+
vi.mocked(fetch).mockResolvedValueOnce({
|
|
22
|
+
ok: true,
|
|
23
|
+
status: 200,
|
|
24
|
+
json: async () => mockResponse,
|
|
25
|
+
} as Response);
|
|
26
|
+
|
|
27
|
+
const result = await client.request('GET', '/test');
|
|
28
|
+
|
|
29
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
30
|
+
'https://api.example.com/test',
|
|
31
|
+
expect.objectContaining({
|
|
32
|
+
method: 'GET',
|
|
33
|
+
headers: expect.objectContaining({
|
|
34
|
+
'Content-Type': 'application/json',
|
|
35
|
+
}),
|
|
36
|
+
credentials: 'include',
|
|
37
|
+
}),
|
|
38
|
+
);
|
|
39
|
+
expect(result).toEqual(mockResponse);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should include auth headers if strategy is provided', async () => {
|
|
43
|
+
const authStrategy = {
|
|
44
|
+
getHeaders: vi.fn().mockResolvedValue({ Authorization: 'Bearer token' }),
|
|
45
|
+
};
|
|
46
|
+
const authenticatedClient = new ApiClient({ baseUrl, authStrategy });
|
|
47
|
+
|
|
48
|
+
vi.mocked(fetch).mockResolvedValueOnce({
|
|
49
|
+
ok: true,
|
|
50
|
+
status: 200,
|
|
51
|
+
json: async () => ({}),
|
|
52
|
+
} as Response);
|
|
53
|
+
|
|
54
|
+
await authenticatedClient.request('GET', '/test');
|
|
55
|
+
|
|
56
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
57
|
+
expect.any(String),
|
|
58
|
+
expect.objectContaining({
|
|
59
|
+
headers: expect.objectContaining({
|
|
60
|
+
Authorization: 'Bearer token',
|
|
61
|
+
}),
|
|
62
|
+
}),
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should handle non-ok responses with JSON error data', async () => {
|
|
67
|
+
const errorData = { message: 'Bad Request' };
|
|
68
|
+
vi.mocked(fetch).mockResolvedValueOnce({
|
|
69
|
+
ok: false,
|
|
70
|
+
status: 400,
|
|
71
|
+
statusText: 'Bad Request',
|
|
72
|
+
text: async () => JSON.stringify(errorData),
|
|
73
|
+
} as Response);
|
|
74
|
+
|
|
75
|
+
await expect(client.request('GET', '/test')).rejects.toThrow(NexicalError);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('should handle non-ok responses with plain text error data', async () => {
|
|
79
|
+
vi.mocked(fetch).mockResolvedValueOnce({
|
|
80
|
+
ok: false,
|
|
81
|
+
status: 500,
|
|
82
|
+
statusText: 'Internal Server Error',
|
|
83
|
+
text: async () => 'Something went wrong',
|
|
84
|
+
} as Response);
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
await client.request('GET', '/test');
|
|
88
|
+
} catch (error) {
|
|
89
|
+
const err = error as NexicalError;
|
|
90
|
+
expect(err).toBeInstanceOf(NexicalError);
|
|
91
|
+
expect(err.status).toBe(500);
|
|
92
|
+
expect(err.data).toBe('Something went wrong');
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('should return empty object for 204 No Content', async () => {
|
|
97
|
+
vi.mocked(fetch).mockResolvedValueOnce({
|
|
98
|
+
ok: true,
|
|
99
|
+
status: 204,
|
|
100
|
+
statusText: 'No Content',
|
|
101
|
+
} as Response);
|
|
102
|
+
|
|
103
|
+
const result = await client.request('DELETE', '/test');
|
|
104
|
+
expect(result).toEqual({});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should allow overriding headers', async () => {
|
|
108
|
+
vi.mocked(fetch).mockResolvedValueOnce({
|
|
109
|
+
ok: true,
|
|
110
|
+
status: 200,
|
|
111
|
+
json: async () => ({}),
|
|
112
|
+
} as Response);
|
|
113
|
+
|
|
114
|
+
await client.request('GET', '/test', null, {
|
|
115
|
+
headers: { 'X-Custom': 'value' },
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
119
|
+
expect.any(String),
|
|
120
|
+
expect.objectContaining({
|
|
121
|
+
headers: expect.objectContaining({
|
|
122
|
+
'X-Custom': 'value',
|
|
123
|
+
'Content-Type': 'application/json',
|
|
124
|
+
}),
|
|
125
|
+
}),
|
|
126
|
+
);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('should handle request body', async () => {
|
|
130
|
+
vi.mocked(fetch).mockResolvedValueOnce({
|
|
131
|
+
ok: true,
|
|
132
|
+
status: 200,
|
|
133
|
+
json: async () => ({}),
|
|
134
|
+
} as Response);
|
|
135
|
+
|
|
136
|
+
const body = { foo: 'bar' };
|
|
137
|
+
await client.request('POST', '/test', body);
|
|
138
|
+
|
|
139
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
140
|
+
expect.any(String),
|
|
141
|
+
expect.objectContaining({
|
|
142
|
+
body: JSON.stringify(body),
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('should not override credentials if already set', async () => {
|
|
148
|
+
vi.mocked(fetch).mockResolvedValueOnce({
|
|
149
|
+
ok: true,
|
|
150
|
+
status: 200,
|
|
151
|
+
json: async () => ({}),
|
|
152
|
+
} as Response);
|
|
153
|
+
|
|
154
|
+
await client.request('GET', '/test', null, {
|
|
155
|
+
credentials: 'omit',
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
159
|
+
expect.any(String),
|
|
160
|
+
expect.objectContaining({
|
|
161
|
+
credentials: 'omit',
|
|
162
|
+
}),
|
|
163
|
+
);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe('NexicalError', () => {
|
|
168
|
+
it('should use error message from data.error if available', () => {
|
|
169
|
+
const error = new NexicalError(400, 'Bad Request', { error: 'Detailed error' });
|
|
170
|
+
expect(error.message).toBe('Detailed error');
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('should use error message from data.message if available', () => {
|
|
174
|
+
const error = new NexicalError(400, 'Bad Request', { message: 'Internal message' });
|
|
175
|
+
expect(error.message).toBe('Internal message');
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('should fallback to statusText if no data message found', () => {
|
|
179
|
+
const error = new NexicalError(400, 'Bad Request', {});
|
|
180
|
+
expect(error.message).toBe('Bad Request');
|
|
181
|
+
});
|
|
182
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import * as index from '../../src/index.ts';
|
|
3
|
+
import { ApiClient, NexicalError } from '../../src/client.ts';
|
|
4
|
+
import { BaseResource } from '../../src/resource.ts';
|
|
5
|
+
|
|
6
|
+
describe('index', () => {
|
|
7
|
+
it('should export client and resource', () => {
|
|
8
|
+
expect(index.ApiClient).toBe(ApiClient);
|
|
9
|
+
expect(index.NexicalError).toBe(NexicalError);
|
|
10
|
+
expect(index.BaseResource).toBe(BaseResource);
|
|
11
|
+
});
|
|
12
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { BaseResource } from '../../src/resource.ts';
|
|
3
|
+
import { ApiClient } from '../../src/client.ts';
|
|
4
|
+
|
|
5
|
+
class TestResource extends BaseResource {
|
|
6
|
+
public async testRequest<T>(
|
|
7
|
+
method: string,
|
|
8
|
+
path: string,
|
|
9
|
+
body?: unknown,
|
|
10
|
+
options?: RequestInit,
|
|
11
|
+
): Promise<T> {
|
|
12
|
+
return this._request<T>(method, path, body, options);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public testBuildQuery(filters: Record<string, unknown>): string {
|
|
16
|
+
return this.buildQuery(filters);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe('BaseResource', () => {
|
|
21
|
+
const mockClient = {
|
|
22
|
+
request: vi.fn(),
|
|
23
|
+
} as unknown as ApiClient;
|
|
24
|
+
|
|
25
|
+
const resource = new TestResource(mockClient);
|
|
26
|
+
|
|
27
|
+
it('should delegate request to ApiClient', async () => {
|
|
28
|
+
const mockData = { success: true };
|
|
29
|
+
vi.mocked(mockClient.request).mockResolvedValueOnce(mockData);
|
|
30
|
+
|
|
31
|
+
const result = await resource.testRequest('GET', '/test', { foo: 'bar' });
|
|
32
|
+
|
|
33
|
+
expect(mockClient.request).toHaveBeenCalledWith('GET', '/test', { foo: 'bar' }, undefined);
|
|
34
|
+
expect(result).toEqual(mockData);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('buildQuery', () => {
|
|
38
|
+
it('should return empty string for empty filters', () => {
|
|
39
|
+
expect(resource.testBuildQuery({})).toBe('');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should build simple query string', () => {
|
|
43
|
+
expect(resource.testBuildQuery({ foo: 'bar', baz: 123 })).toBe('?foo=bar&baz=123');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should handle nested objects with double underscores', () => {
|
|
47
|
+
expect(
|
|
48
|
+
resource.testBuildQuery({
|
|
49
|
+
user: {
|
|
50
|
+
name: 'John',
|
|
51
|
+
profile: {
|
|
52
|
+
age: 30,
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
active: true,
|
|
56
|
+
}),
|
|
57
|
+
).toBe('?user__name=John&user__profile__age=30&active=true');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should ignore null and undefined values', () => {
|
|
61
|
+
expect(
|
|
62
|
+
resource.testBuildQuery({
|
|
63
|
+
foo: 'bar',
|
|
64
|
+
baz: null,
|
|
65
|
+
qux: undefined,
|
|
66
|
+
empty: '',
|
|
67
|
+
}),
|
|
68
|
+
).toBe('?foo=bar&empty=');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('should handle arrays', () => {
|
|
72
|
+
expect(
|
|
73
|
+
resource.testBuildQuery({
|
|
74
|
+
tags: ['a', 'b', 'c'],
|
|
75
|
+
}),
|
|
76
|
+
).toBe('?tags=a%2Cb%2Cc');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('should handle complex mixed objects', () => {
|
|
80
|
+
const filters = {
|
|
81
|
+
where: {
|
|
82
|
+
status: 'active',
|
|
83
|
+
type: 'user',
|
|
84
|
+
},
|
|
85
|
+
orderBy: 'createdAt',
|
|
86
|
+
limit: 10,
|
|
87
|
+
};
|
|
88
|
+
const expected = '?where__status=active&where__type=user&orderBy=createdAt&limit=10';
|
|
89
|
+
expect(resource.testBuildQuery(filters)).toBe(expected);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ESNext",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"esModuleInterop": true
|
|
11
|
+
},
|
|
12
|
+
"include": ["src/**/*"],
|
|
13
|
+
"exclude": ["node_modules", "**/*.test.ts"]
|
|
14
|
+
}
|