@slyxup/core 0.2.0 → 0.2.1

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.
@@ -0,0 +1,4 @@
1
+
2
+ > @slyxup/core@0.2.1 build /home/runner/work/stack/stack/packages/core
3
+ > tsc -p tsconfig.json
4
+
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @slyxup/core
2
2
 
3
+ ## 0.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [`2011599`](https://github.com/slyxup/stack/commit/2011599a9c989560e34bcea3147c18923e15962d) - CLI: new `slyxup auth` command group using @slyxup/core SDK — `auth signup`, `auth signin`, `auth verify` (email verification), `auth oauth` (opens Google/GitHub in browser). Browser open is non-blocking. CLI now depends on @slyxup/core.
8
+
3
9
  ## 0.2.0
4
10
 
5
11
  ### Minor Changes
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 SlyxUp
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json CHANGED
@@ -1,14 +1,10 @@
1
1
  {
2
2
  "name": "@slyxup/core",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "@slyxup/core — SlyxUp SDK",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
8
- "scripts": {
9
- "build": "tsc -p tsconfig.json",
10
- "typecheck": "tsc --noEmit"
11
- },
12
8
  "repository": {
13
9
  "type": "git",
14
10
  "url": "git+https://github.com/slyxup/stack.git",
@@ -22,5 +18,13 @@
22
18
  "license": "MIT",
23
19
  "publishConfig": {
24
20
  "access": "public"
21
+ },
22
+ "devDependencies": {
23
+ "vitest": "^4.1.11"
24
+ },
25
+ "scripts": {
26
+ "build": "tsc -p tsconfig.json",
27
+ "typecheck": "tsc --noEmit",
28
+ "test": "vitest run --passWithNoTests"
25
29
  }
26
- }
30
+ }
@@ -0,0 +1,249 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+ import { SlyxupClient } from '../src/client.js';
3
+ import { SlyxupError, UnauthorizedError, NetworkError, RateLimitError } from '../src/errors.js';
4
+
5
+ function mockFetch(
6
+ data: unknown,
7
+ init?: { ok?: boolean; status?: number; headers?: Record<string, string> }
8
+ ) {
9
+ const fn = vi.fn().mockResolvedValue({
10
+ ok: init?.ok ?? true,
11
+ status: init?.status ?? 200,
12
+ headers: {
13
+ get: (key: string) => {
14
+ const lower = key.toLowerCase();
15
+ for (const [k, v] of Object.entries(init?.headers ?? {})) {
16
+ if (k.toLowerCase() === lower) return v;
17
+ }
18
+ return null;
19
+ },
20
+ },
21
+ json: () => Promise.resolve(data),
22
+ });
23
+ global.fetch = fn;
24
+ return fn;
25
+ }
26
+
27
+ describe('SlyxupClient', () => {
28
+ beforeEach(() => {
29
+ vi.clearAllMocks();
30
+ });
31
+
32
+ describe('constructor', () => {
33
+ it('should use default apiUrl', () => {
34
+ mockFetch({ ok: true });
35
+ const client = new SlyxupClient();
36
+ expect(client.apiUrl).toBe('https://auth.slyxup.online');
37
+ });
38
+
39
+ it('should strip trailing slash from apiUrl', () => {
40
+ mockFetch({ ok: true });
41
+ const client = new SlyxupClient({ apiUrl: 'http://localhost:3000/' });
42
+ expect(client.apiUrl).toBe('http://localhost:3000');
43
+ });
44
+
45
+ it('should store publishableKey', () => {
46
+ mockFetch({ ok: true });
47
+ const client = new SlyxupClient({ publishableKey: 'pk_test_123' });
48
+ expect(client.publishableKey).toBe('pk_test_123');
49
+ });
50
+ });
51
+
52
+ describe('auth.signUp', () => {
53
+ it('should POST to /v1/auth/sign-up with JSON body', async () => {
54
+ const fn = mockFetch({ ok: true, user: { id: 'u1', email: 'a@b.com' } });
55
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
56
+ const res = await client.auth.signUp({
57
+ email: 'a@b.com',
58
+ password: '12345678',
59
+ firstName: 'Test',
60
+ });
61
+ expect(fn).toHaveBeenCalledWith(
62
+ 'http://localhost/v1/auth/sign-up',
63
+ expect.objectContaining({
64
+ method: 'POST',
65
+ body: JSON.stringify({ email: 'a@b.com', password: '12345678', firstName: 'Test' }),
66
+ })
67
+ );
68
+ expect(res).toEqual({ ok: true, user: { id: 'u1', email: 'a@b.com' } });
69
+ });
70
+
71
+ it('should throw SlyxupError when response has no user', async () => {
72
+ mockFetch({ ok: true, error: 'Invalid email' });
73
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
74
+ await expect(
75
+ client.auth.signUp({ email: 'bad', password: '12345678' })
76
+ ).rejects.toThrow(SlyxupError);
77
+ });
78
+ });
79
+
80
+ describe('auth.signIn', () => {
81
+ it('should POST to /v1/auth/sign-in', async () => {
82
+ const fn = mockFetch({ ok: true, user: { id: 'u1', email: 'a@b.com' } });
83
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
84
+ const res = await client.auth.signIn({ email: 'a@b.com', password: 'pass' });
85
+ expect(fn).toHaveBeenCalledWith(
86
+ 'http://localhost/v1/auth/sign-in',
87
+ expect.objectContaining({ method: 'POST' })
88
+ );
89
+ expect(res).toEqual({ ok: true, user: { id: 'u1', email: 'a@b.com' } });
90
+ });
91
+
92
+ it('should throw UnauthorizedError on 401', async () => {
93
+ mockFetch({ ok: false, error: 'Bad credentials' }, { ok: false, status: 401 });
94
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
95
+ await expect(
96
+ client.auth.signIn({ email: 'a@b.com', password: 'wrong' })
97
+ ).rejects.toThrow(UnauthorizedError);
98
+ });
99
+
100
+ it('should throw SlyxupError when response has no user', async () => {
101
+ mockFetch({ ok: true, error: 'No user' });
102
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
103
+ await expect(
104
+ client.auth.signIn({ email: 'a@b.com', password: 'pass' })
105
+ ).rejects.toThrow(SlyxupError);
106
+ });
107
+ });
108
+
109
+ describe('auth.signOut', () => {
110
+ it('should POST to /v1/auth/sign-out', async () => {
111
+ const fn = mockFetch({ ok: true });
112
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
113
+ const res = await client.auth.signOut();
114
+ expect(fn).toHaveBeenCalledWith(
115
+ 'http://localhost/v1/auth/sign-out',
116
+ expect.objectContaining({ method: 'POST' })
117
+ );
118
+ expect(res).toEqual({ ok: true });
119
+ });
120
+ });
121
+
122
+ describe('sessions.get', () => {
123
+ it('should GET /v1/session', async () => {
124
+ const data = { ok: true, user: { id: 'u1', email: 'a@b.com' }, session: { id: 's1', expiresAt: '2030' } };
125
+ const fn = mockFetch(data);
126
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
127
+ const res = await client.sessions.get();
128
+ expect(fn).toHaveBeenCalledWith('http://localhost/v1/session', expect.anything());
129
+ expect(res).toEqual(data);
130
+ });
131
+
132
+ it('should throw UnauthorizedError when no session in response', async () => {
133
+ mockFetch({ ok: true, error: 'No session' });
134
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
135
+ await expect(client.sessions.get()).rejects.toThrow(UnauthorizedError);
136
+ });
137
+
138
+ it('should throw NetworkError when fetch rejects', async () => {
139
+ global.fetch = vi.fn().mockRejectedValue(new Error('fetch failed'));
140
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
141
+ await expect(client.sessions.get()).rejects.toThrow(NetworkError);
142
+ });
143
+ });
144
+
145
+ describe('users.me', () => {
146
+ it('should GET /v1/user', async () => {
147
+ const user = { id: 'u1', email: 'a@b.com', firstName: 'Test', lastName: 'User', createdAt: '', updatedAt: '', projectId: null, emailVerified: true, avatarUrl: null, preferences: null };
148
+ const fn = mockFetch({ ok: true, user });
149
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
150
+ const res = await client.users.me();
151
+ expect(fn).toHaveBeenCalledWith('http://localhost/v1/user', expect.anything());
152
+ expect(res.user).toEqual(user);
153
+ });
154
+
155
+ it('should throw UnauthorizedError when no user', async () => {
156
+ mockFetch({ ok: true, error: 'Not found' });
157
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
158
+ await expect(client.users.me()).rejects.toThrow(UnauthorizedError);
159
+ });
160
+ });
161
+
162
+ describe('users.update', () => {
163
+ it('should PATCH /v1/user with JSON body', async () => {
164
+ const fn = mockFetch({ ok: true, user: { id: 'u1', firstName: 'Jane' } });
165
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
166
+ await client.users.update({ firstName: 'Jane' });
167
+ expect(fn).toHaveBeenCalledWith(
168
+ 'http://localhost/v1/user',
169
+ expect.objectContaining({ method: 'PATCH', body: JSON.stringify({ firstName: 'Jane' }) })
170
+ );
171
+ });
172
+ });
173
+
174
+ describe('users.delete', () => {
175
+ it('should DELETE /v1/user', async () => {
176
+ const fn = mockFetch({ ok: true });
177
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
178
+ const res = await client.users.delete();
179
+ expect(fn).toHaveBeenCalledWith('http://localhost/v1/user', expect.objectContaining({ method: 'DELETE' }));
180
+ expect(res).toEqual({ ok: true });
181
+ });
182
+ });
183
+
184
+ describe('cookie jar', () => {
185
+ it('should capture Set-Cookie and forward as Cookie header', async () => {
186
+ const fn = vi.fn()
187
+ .mockResolvedValueOnce({
188
+ ok: true,
189
+ status: 200,
190
+ headers: {
191
+ get: (key: string) => key.toLowerCase() === 'set-cookie' ? 'slyxup_session=abc123; Path=/; HttpOnly' : null,
192
+ },
193
+ json: () => Promise.resolve({ ok: true, user: { id: 'u1', email: 'a@b.com' } }),
194
+ })
195
+ .mockResolvedValueOnce({
196
+ ok: true,
197
+ status: 200,
198
+ headers: { get: () => null },
199
+ json: () => Promise.resolve({ ok: true, user: { id: 'u1', email: 'a@b.com' }, session: { id: 's1', expiresAt: '2030' } }),
200
+ });
201
+ global.fetch = fn;
202
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
203
+ await client.auth.signIn({ email: 'a@b.com', password: 'pass' });
204
+ await client.sessions.get();
205
+ expect(fn).toHaveBeenCalledTimes(2);
206
+ const secondCall = fn.mock.calls[1] as any[];
207
+ expect(secondCall[1].headers).toMatchObject({ Cookie: 'slyxup_session=abc123' });
208
+ });
209
+ });
210
+
211
+ describe('error handling', () => {
212
+ it('should throw RateLimitError on 429', async () => {
213
+ mockFetch({ error: 'rate limited' }, { ok: false, status: 429 });
214
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
215
+ await expect(client.sessions.get()).rejects.toThrow(RateLimitError);
216
+ });
217
+
218
+ it('should throw NetworkError when fetch rejects', async () => {
219
+ global.fetch = vi.fn().mockRejectedValue(new TypeError('Failed to fetch'));
220
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
221
+ await expect(client.auth.signIn({ email: 'a', password: 'b' })).rejects.toThrow(NetworkError);
222
+ });
223
+
224
+ it('should throw SlyxupError on 500 with error message', async () => {
225
+ mockFetch({ error: 'Server error' }, { ok: false, status: 500 });
226
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
227
+ try {
228
+ await client.sessions.get();
229
+ expect.fail('Should have thrown');
230
+ } catch (e) {
231
+ expect(e).toBeInstanceOf(SlyxupError);
232
+ expect((e as SlyxupError).status).toBe(500);
233
+ expect((e as SlyxupError).code).toBe('api_error');
234
+ expect((e as SlyxupError).message).toBe('Server error');
235
+ }
236
+ });
237
+
238
+ it('should handle invalid JSON gracefully', async () => {
239
+ global.fetch = vi.fn().mockResolvedValue({
240
+ ok: true,
241
+ status: 200,
242
+ headers: { get: () => null },
243
+ json: () => Promise.reject(new Error('not json')),
244
+ });
245
+ const client = new SlyxupClient({ apiUrl: 'http://localhost' });
246
+ await expect(client.sessions.get()).rejects.toThrow(UnauthorizedError);
247
+ });
248
+ });
249
+ });
@@ -0,0 +1,109 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import {
3
+ SlyxupError,
4
+ UnauthorizedError,
5
+ ValidationError,
6
+ NetworkError,
7
+ RateLimitError,
8
+ } from '../src/errors.js';
9
+
10
+ describe('SlyxupError', () => {
11
+ it('should set message, status, code, and name', () => {
12
+ const err = new SlyxupError('test', 500, 'test_error');
13
+ expect(err.message).toBe('test');
14
+ expect(err.status).toBe(500);
15
+ expect(err.code).toBe('test_error');
16
+ expect(err.name).toBe('SlyxupError');
17
+ });
18
+
19
+ it('should be instanceof Error', () => {
20
+ const err = new SlyxupError('test', 400, 'err');
21
+ expect(err).toBeInstanceOf(Error);
22
+ });
23
+
24
+ it('should be instanceof SlyxupError', () => {
25
+ const err = new SlyxupError('test', 400, 'err');
26
+ expect(err).toBeInstanceOf(SlyxupError);
27
+ });
28
+ });
29
+
30
+ describe('UnauthorizedError', () => {
31
+ it('should have default values', () => {
32
+ const err = new UnauthorizedError();
33
+ expect(err.message).toBe('Unauthorized');
34
+ expect(err.status).toBe(401);
35
+ expect(err.code).toBe('unauthorized');
36
+ expect(err.name).toBe('UnauthorizedError');
37
+ });
38
+
39
+ it('should accept custom message', () => {
40
+ const err = new UnauthorizedError('Custom');
41
+ expect(err.message).toBe('Custom');
42
+ expect(err.status).toBe(401);
43
+ });
44
+
45
+ it('should be instanceof SlyxupError', () => {
46
+ expect(new UnauthorizedError()).toBeInstanceOf(SlyxupError);
47
+ expect(new UnauthorizedError()).toBeInstanceOf(Error);
48
+ });
49
+ });
50
+
51
+ describe('ValidationError', () => {
52
+ it('should have default values', () => {
53
+ const err = new ValidationError();
54
+ expect(err.message).toBe('Validation failed');
55
+ expect(err.status).toBe(400);
56
+ expect(err.code).toBe('validation_error');
57
+ expect(err.name).toBe('ValidationError');
58
+ });
59
+
60
+ it('should accept custom message', () => {
61
+ const err = new ValidationError('Bad input');
62
+ expect(err.message).toBe('Bad input');
63
+ expect(err.status).toBe(400);
64
+ });
65
+
66
+ it('should be instanceof SlyxupError', () => {
67
+ expect(new ValidationError()).toBeInstanceOf(SlyxupError);
68
+ });
69
+ });
70
+
71
+ describe('NetworkError', () => {
72
+ it('should have default values', () => {
73
+ const err = new NetworkError();
74
+ expect(err.message).toBe('Network request failed');
75
+ expect(err.status).toBe(0);
76
+ expect(err.code).toBe('network_error');
77
+ expect(err.name).toBe('NetworkError');
78
+ });
79
+
80
+ it('should accept custom message', () => {
81
+ const err = new NetworkError('Timeout');
82
+ expect(err.message).toBe('Timeout');
83
+ expect(err.status).toBe(0);
84
+ });
85
+
86
+ it('should be instanceof SlyxupError', () => {
87
+ expect(new NetworkError()).toBeInstanceOf(SlyxupError);
88
+ });
89
+ });
90
+
91
+ describe('RateLimitError', () => {
92
+ it('should have default values', () => {
93
+ const err = new RateLimitError();
94
+ expect(err.message).toBe('Too many requests');
95
+ expect(err.status).toBe(429);
96
+ expect(err.code).toBe('rate_limited');
97
+ expect(err.name).toBe('RateLimitError');
98
+ });
99
+
100
+ it('should accept custom message', () => {
101
+ const err = new RateLimitError('Slow down');
102
+ expect(err.message).toBe('Slow down');
103
+ expect(err.status).toBe(429);
104
+ });
105
+
106
+ it('should be instanceof SlyxupError', () => {
107
+ expect(new RateLimitError()).toBeInstanceOf(SlyxupError);
108
+ });
109
+ });
@@ -0,0 +1,2 @@
1
+ import { defineConfig } from 'vitest/config';
2
+ export default defineConfig({ test: { globals: true, environment: 'node' } });