@neondatabase/neon-js 0.1.0-alpha.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.
- package/CHANGELOG.md +86 -0
- package/LICENSE +201 -0
- package/README.md +388 -0
- package/dist/cli/index.js +145 -0
- package/dist/index.d.ts +147 -0
- package/dist/index.js +1360 -0
- package/package.json +60 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.0-alpha.1] - 2025-10-24
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
#### Core SDK
|
|
15
|
+
- Unified TypeScript SDK for Neon Auth and Neon Data API
|
|
16
|
+
- `NeonClient` class extending PostgrestClient for seamless database queries
|
|
17
|
+
- `createClient()` factory function for easy client initialization
|
|
18
|
+
- Automatic token injection via auth-aware fetch wrapper
|
|
19
|
+
- Full TypeScript support with strict mode enabled
|
|
20
|
+
- Support for custom headers, fetch implementation, and schema configuration
|
|
21
|
+
|
|
22
|
+
#### Authentication
|
|
23
|
+
- Complete Supabase-compatible `AuthClient` interface for easy migration
|
|
24
|
+
- Stack Auth adapter with 25+ authentication methods:
|
|
25
|
+
- Email/password authentication (`signUp`, `signInWithPassword`)
|
|
26
|
+
- OAuth social login (`signInWithOAuth`) with all Stack Auth providers
|
|
27
|
+
- Magic link/OTP authentication (`signInWithOtp`, `verifyOtp`)
|
|
28
|
+
- Session management (`getSession`, `refreshSession`, `setSession`, `signOut`)
|
|
29
|
+
- User management (`getUser`, `updateUser`, `getClaims`)
|
|
30
|
+
- Identity linking (`linkIdentity`, `unlinkIdentity`, `getUserIdentities`)
|
|
31
|
+
- Password reset (`resetPasswordForEmail`, `resend`)
|
|
32
|
+
- OAuth callback handling (`exchangeCodeForSession`)
|
|
33
|
+
- Reauthentication (`reauthenticate`)
|
|
34
|
+
- State change monitoring (`onAuthStateChange`)
|
|
35
|
+
- Session caching optimization leveraging Stack Auth internals (<5ms cached reads)
|
|
36
|
+
- JWT token validation with Zod schemas
|
|
37
|
+
- Comprehensive error handling with detailed error messages
|
|
38
|
+
|
|
39
|
+
#### Environment Support
|
|
40
|
+
- Full browser support with cross-tab authentication synchronization via BroadcastChannel
|
|
41
|
+
- Node.js support with automatic graceful degradation of browser-only features
|
|
42
|
+
- Server-side support with secret key authentication
|
|
43
|
+
- Memory and cookie-based token storage options
|
|
44
|
+
|
|
45
|
+
#### CLI Tool
|
|
46
|
+
- `neon-js` command-line tool for TypeScript type generation
|
|
47
|
+
- Database schema introspection using Supabase's postgres-meta
|
|
48
|
+
- Multi-schema support
|
|
49
|
+
- PostgREST v9 compatibility mode
|
|
50
|
+
- Configurable query timeouts
|
|
51
|
+
- Custom output path support
|
|
52
|
+
|
|
53
|
+
#### Testing Infrastructure
|
|
54
|
+
- Comprehensive test suite with 10+ test files covering:
|
|
55
|
+
- Authentication flows
|
|
56
|
+
- Session management
|
|
57
|
+
- Error handling
|
|
58
|
+
- OAuth flows (Node.js and browser environments)
|
|
59
|
+
- OTP/magic link authentication
|
|
60
|
+
- User management
|
|
61
|
+
- Supabase API compatibility
|
|
62
|
+
- MSW (Mock Service Worker) for HTTP mocking
|
|
63
|
+
- Vitest test framework with Node.js and Bun runtime support
|
|
64
|
+
- Test utilities and shared fixtures
|
|
65
|
+
|
|
66
|
+
#### Documentation
|
|
67
|
+
- Comprehensive README with quick start guide
|
|
68
|
+
- Real-world migration example (Todo Guardian Pro)
|
|
69
|
+
- Supabase migration guide
|
|
70
|
+
- API reference for all authentication methods
|
|
71
|
+
- Architecture documentation
|
|
72
|
+
- Performance benchmarks
|
|
73
|
+
- Stack Auth vs Supabase feature comparison
|
|
74
|
+
|
|
75
|
+
### Known Limitations
|
|
76
|
+
|
|
77
|
+
The following authentication methods are not supported by Stack Auth and return detailed error messages:
|
|
78
|
+
- `signInWithIdToken()` - Stack Auth uses OAuth redirect flows only
|
|
79
|
+
- `signInWithSSO()` - SAML SSO not supported (OAuth social providers only)
|
|
80
|
+
- `signInWithWeb3()` - Blockchain/crypto wallet authentication not supported
|
|
81
|
+
- `signInAnonymously()` - Anonymous authentication not supported
|
|
82
|
+
|
|
83
|
+
Password updates via `updateUser({ password })` require old password or password reset flow due to Stack Auth security requirements.
|
|
84
|
+
|
|
85
|
+
[unreleased]: https://github.com/neondatabase-labs/neon-js/compare/v0.1.0-alpha.1...HEAD
|
|
86
|
+
[0.1.0-alpha.1]: https://github.com/neondatabase-labs/neon-js/releases/tag/v0.1.0-alpha.1
|
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 Support. 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 Pedro Figueiredo
|
|
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,388 @@
|
|
|
1
|
+
# neon-js
|
|
2
|
+
|
|
3
|
+
A unified TypeScript SDK for Neon services, providing seamless integration with **Neon Auth** (authentication service) and **Neon Data API** (PostgreSQL database queries). Built with a Supabase-compatible interface for easy migration and familiar developer experience.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Unified SDK**: Single client for Neon Auth and Neon Data API
|
|
8
|
+
- **Supabase-Compatible**: Drop-in replacement for easy migration from Supabase
|
|
9
|
+
- **Adapter Pattern**: Pluggable authentication providers (Stack Auth included)
|
|
10
|
+
- **Automatic Token Injection**: Auth-aware fetch wrapper for seamless API calls
|
|
11
|
+
- **TypeScript**: Full type safety with strict mode enabled
|
|
12
|
+
- **Performance Optimized**: Leverages Stack Auth's internal session cache for <5ms session reads
|
|
13
|
+
- **CLI Tool**: Generate TypeScript types from your database schema
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
### From npm
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @neondatabase/neon-js
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Using neon-js
|
|
24
|
+
|
|
25
|
+
1. Create a Neon project with Data API enabled
|
|
26
|
+
2. Copy env vars from the Data API page and set them in your environment
|
|
27
|
+
```bash
|
|
28
|
+
VITE_STACK_PROJECT_ID=
|
|
29
|
+
VITE_STACK_PUBLISHABLE_CLIENT_KEY=
|
|
30
|
+
VITE_NEON_DATA_API_URL=
|
|
31
|
+
```
|
|
32
|
+
3. Instantiate `createClient` with the correct parameters
|
|
33
|
+
```typescript
|
|
34
|
+
import { createClient } from 'neon-js';
|
|
35
|
+
|
|
36
|
+
const client = createClient({
|
|
37
|
+
url: 'https://your-api.com',
|
|
38
|
+
auth: {
|
|
39
|
+
projectId: 'your-project-id',
|
|
40
|
+
publishableClientKey: 'pk_...',
|
|
41
|
+
tokenStore: 'cookie', // or 'memory'
|
|
42
|
+
},
|
|
43
|
+
options: {
|
|
44
|
+
// Optional: custom configuration
|
|
45
|
+
global: {
|
|
46
|
+
headers: { 'X-Custom-Header': 'value' },
|
|
47
|
+
},
|
|
48
|
+
db: {
|
|
49
|
+
schema: 'public',
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Migrating from Supabase
|
|
56
|
+
|
|
57
|
+
neon-js provides a Supabase-compatible API, making migration straightforward with minimal code changes. Here's a real-world migration example from the [Todo Guardian Pro project](https://github.com/pffigueiredo/todo-guardian-pro/pull/1).
|
|
58
|
+
|
|
59
|
+
### Migration Steps
|
|
60
|
+
|
|
61
|
+
**1. Update Dependencies**
|
|
62
|
+
|
|
63
|
+
Replace `@supabase/supabase-js` with `neon-js` in your `package.json`:
|
|
64
|
+
|
|
65
|
+
```diff
|
|
66
|
+
- "@supabase/supabase-js": "^2.74.0"
|
|
67
|
+
+ "neon-js": "^0.0.0"
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**2. Update Environment Variables**
|
|
71
|
+
|
|
72
|
+
Replace Supabase environment variables with Neon equivalents:
|
|
73
|
+
|
|
74
|
+
```diff
|
|
75
|
+
- VITE_SUPABASE_PROJECT_ID="..."
|
|
76
|
+
- VITE_SUPABASE_PUBLISHABLE_KEY="..."
|
|
77
|
+
- VITE_SUPABASE_URL="https://xxx.supabase.co"
|
|
78
|
+
+ VITE_NEON_DATA_API_URL="https://xxx.apirest.c-2.us-east-1.aws.neon.tech/neondb/rest/v1"
|
|
79
|
+
+ VITE_STACK_PROJECT_ID="..."
|
|
80
|
+
+ VITE_STACK_PUBLISHABLE_CLIENT_KEY="..."
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Get these values from your Neon dashboard:
|
|
84
|
+
- **Data API URL**: Available in the Neon console under "Data API"
|
|
85
|
+
- **Stack Auth credentials**: Project ID and Publishable Client Key from Neon Auth setup
|
|
86
|
+
|
|
87
|
+
**3. Update Client Initialization**
|
|
88
|
+
|
|
89
|
+
Update your client configuration to use neon-js:
|
|
90
|
+
|
|
91
|
+
```diff
|
|
92
|
+
- import { createClient } from '@supabase/supabase-js';
|
|
93
|
+
+ import { createClient } from 'neon-js';
|
|
94
|
+
|
|
95
|
+
- export const supabase = createClient(
|
|
96
|
+
- import.meta.env.VITE_SUPABASE_URL,
|
|
97
|
+
- import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY
|
|
98
|
+
- );
|
|
99
|
+
+ export const supabase = createClient({
|
|
100
|
+
+ url: import.meta.env.VITE_NEON_DATA_API_URL,
|
|
101
|
+
+ auth: {
|
|
102
|
+
+ tokenStore: 'cookie',
|
|
103
|
+
+ projectId: import.meta.env.VITE_STACK_PROJECT_ID,
|
|
104
|
+
+ publishableClientKey: import.meta.env.VITE_STACK_PUBLISHABLE_CLIENT_KEY,
|
|
105
|
+
+ },
|
|
106
|
+
+ });
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
**4. Done!**
|
|
110
|
+
|
|
111
|
+
That's it! The rest of your code remains unchanged. All authentication methods (`signInWithPassword`, `signOut`, `getUser`, etc.) and database queries (`from().select()`, etc.) work exactly the same.
|
|
112
|
+
|
|
113
|
+
### What Stays the Same
|
|
114
|
+
|
|
115
|
+
- ✅ All authentication method signatures
|
|
116
|
+
- ✅ All database query methods
|
|
117
|
+
- ✅ Session management APIs
|
|
118
|
+
- ✅ User management APIs
|
|
119
|
+
- ✅ OAuth flows
|
|
120
|
+
- ✅ Error handling patterns
|
|
121
|
+
|
|
122
|
+
### Real-World Example
|
|
123
|
+
|
|
124
|
+
See the complete migration in this PR: [pffigueiredo/todo-guardian-pro#1](https://github.com/pffigueiredo/todo-guardian-pro/pull/1)
|
|
125
|
+
|
|
126
|
+
The migration changed only:
|
|
127
|
+
- 1 dependency in `package.json`
|
|
128
|
+
- 3 environment variables in `.env`
|
|
129
|
+
- Client initialization in `src/integrations/supabase/client.ts`
|
|
130
|
+
|
|
131
|
+
Everything else stayed the same!
|
|
132
|
+
|
|
133
|
+
## Quick Start
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
import { createClient } from 'neon-js';
|
|
137
|
+
|
|
138
|
+
// Create client with Stack Auth integration
|
|
139
|
+
const client = createClient({
|
|
140
|
+
url: 'https://your-api.com',
|
|
141
|
+
auth: {
|
|
142
|
+
projectId: 'your-project-id',
|
|
143
|
+
publishableClientKey: 'pk_...',
|
|
144
|
+
tokenStore: 'cookie', // or 'memory'
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// Sign in
|
|
149
|
+
await client.auth.signInWithPassword({
|
|
150
|
+
email: 'user@example.com',
|
|
151
|
+
password: 'password123',
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// Get current session
|
|
155
|
+
const { data } = await client.auth.getSession();
|
|
156
|
+
|
|
157
|
+
// Make authenticated API calls (tokens injected automatically)
|
|
158
|
+
const { data: items } = await client.from('items').select();
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Environment Support
|
|
162
|
+
|
|
163
|
+
The SDK works in both browser and Node.js environments:
|
|
164
|
+
|
|
165
|
+
### Browser
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
// Full feature support including cross-tab sync
|
|
169
|
+
import { createClient } from 'neon-js';
|
|
170
|
+
|
|
171
|
+
const client = createClient({
|
|
172
|
+
url: 'https://your-api.com',
|
|
173
|
+
auth: {
|
|
174
|
+
projectId: 'your-project-id',
|
|
175
|
+
publishableClientKey: 'pk_...',
|
|
176
|
+
tokenStore: 'cookie', // Use cookies in browser
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Node.js
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
// All auth methods work, cross-tab features automatically disabled
|
|
185
|
+
import { createClient } from 'neon-js';
|
|
186
|
+
|
|
187
|
+
const client = createClient({
|
|
188
|
+
url: 'https://your-api.com',
|
|
189
|
+
auth: {
|
|
190
|
+
projectId: 'your-project-id',
|
|
191
|
+
publishableClientKey: 'pk_...',
|
|
192
|
+
tokenStore: 'memory', // Use memory storage in Node.js
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Server-Side (with secret key)
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
import { createClient } from 'neon-js';
|
|
201
|
+
|
|
202
|
+
const client = createClient({
|
|
203
|
+
url: 'https://your-api.com',
|
|
204
|
+
auth: {
|
|
205
|
+
projectId: 'your-project-id',
|
|
206
|
+
secretServerKey: 'sk_...', // Server key for server-side operations
|
|
207
|
+
tokenStore: 'memory',
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Architecture
|
|
213
|
+
|
|
214
|
+
- **NeonClient**: Unified client for Neon Auth and Data API (extends PostgrestClient)
|
|
215
|
+
- **AuthClient Interface**: Supabase-compatible authentication interface for easy migration
|
|
216
|
+
- **Adapter Pattern**: Pluggable authentication providers (Stack Auth included)
|
|
217
|
+
- **Factory Pattern**: `createClient()` handles initialization and wiring
|
|
218
|
+
- **Performance Optimized**: Session caching, automatic token injection, and retry logic
|
|
219
|
+
|
|
220
|
+
## Development
|
|
221
|
+
|
|
222
|
+
Install dependencies:
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
bun install
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
Run development server with watch mode:
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
bun dev
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Build the library:
|
|
235
|
+
|
|
236
|
+
```bash
|
|
237
|
+
bun build
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
Run tests:
|
|
241
|
+
|
|
242
|
+
```bash
|
|
243
|
+
bun test
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
Type check:
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
bun typecheck
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Publishing
|
|
253
|
+
|
|
254
|
+
Bump version and publish to npm:
|
|
255
|
+
|
|
256
|
+
```bash
|
|
257
|
+
bun release
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## Project Structure
|
|
261
|
+
|
|
262
|
+
```
|
|
263
|
+
src/
|
|
264
|
+
├── auth/
|
|
265
|
+
│ ├── auth-interface.ts # Core AuthClient interface
|
|
266
|
+
│ ├── utils.ts # Shared utility functions
|
|
267
|
+
│ ├── __tests__/ # Comprehensive test suite
|
|
268
|
+
│ │ ├── auth-flows.test.ts
|
|
269
|
+
│ │ ├── session-management.test.ts
|
|
270
|
+
│ │ ├── error-handling.test.ts
|
|
271
|
+
│ │ ├── oauth.test.ts
|
|
272
|
+
│ │ ├── oauth.browser.test.ts
|
|
273
|
+
│ │ ├── otp.test.ts
|
|
274
|
+
│ │ ├── user-management.test.ts
|
|
275
|
+
│ │ ├── stack-auth-helpers.test.ts
|
|
276
|
+
│ │ ├── supabase-compatibility.test.ts
|
|
277
|
+
│ │ ├── msw-setup.ts
|
|
278
|
+
│ │ ├── msw-handlers.ts
|
|
279
|
+
│ │ └── README.md
|
|
280
|
+
│ └── adapters/
|
|
281
|
+
│ └── stack-auth/
|
|
282
|
+
│ ├── stack-auth-adapter.ts # Stack Auth implementation (2000+ lines)
|
|
283
|
+
│ ├── stack-auth-types.ts # Type definitions and interfaces
|
|
284
|
+
│ ├── stack-auth-schemas.ts # Zod schemas for JWT validation
|
|
285
|
+
│ └── stack-auth-helpers.ts # Helper utilities
|
|
286
|
+
├── client/
|
|
287
|
+
│ ├── neon-client.ts # NeonClient class (extends PostgrestClient)
|
|
288
|
+
│ ├── client-factory.ts # createClient() factory function
|
|
289
|
+
│ ├── neon-client.test.ts # Client tests
|
|
290
|
+
│ └── fetch-with-auth.ts # Auth-aware fetch wrapper
|
|
291
|
+
├── cli/
|
|
292
|
+
│ ├── index.ts # CLI entry point (bin: neon-js)
|
|
293
|
+
│ ├── commands/
|
|
294
|
+
│ │ ├── gen-types.ts # Type generation command
|
|
295
|
+
│ │ └── generate-types.ts # Core type generation logic
|
|
296
|
+
│ └── utils/
|
|
297
|
+
│ └── parse-duration.ts # Duration parsing utility
|
|
298
|
+
└── index.ts # Public exports
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
## CLI Tool: Generate Types
|
|
302
|
+
|
|
303
|
+
The `neon-js` package includes a CLI tool for generating TypeScript types from your database schema.
|
|
304
|
+
|
|
305
|
+
### Installation
|
|
306
|
+
|
|
307
|
+
No installation required! Use via npx:
|
|
308
|
+
|
|
309
|
+
```bash
|
|
310
|
+
npx neon-js gen-types --db-url "postgresql://..."
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
### Usage
|
|
314
|
+
|
|
315
|
+
```bash
|
|
316
|
+
npx neon-js gen-types --db-url <url> [flags]
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
#### Required Flags
|
|
320
|
+
|
|
321
|
+
- `--db-url <url>` - Database connection string
|
|
322
|
+
|
|
323
|
+
#### Optional Flags
|
|
324
|
+
|
|
325
|
+
- `--output <path>`, `-o <path>` - Output file path (default: `database.types.ts`)
|
|
326
|
+
- `--schema <name>`, `-s <name>` - Schema to include (can be used multiple times, default: `public`)
|
|
327
|
+
- `--postgrest-v9-compat` - Disable one-to-one relationship detection
|
|
328
|
+
- `--query-timeout <duration>` - Query timeout (default: `15s`, format: `30s`, `1m`, `90s`)
|
|
329
|
+
|
|
330
|
+
#### Examples
|
|
331
|
+
|
|
332
|
+
```bash
|
|
333
|
+
# Basic usage
|
|
334
|
+
npx neon-js gen-types --db-url "postgresql://user:pass@host:5432/db"
|
|
335
|
+
|
|
336
|
+
# Custom output path
|
|
337
|
+
npx neon-js gen-types --db-url "postgresql://..." --output src/types/db.ts
|
|
338
|
+
|
|
339
|
+
# Multiple schemas
|
|
340
|
+
npx neon-js gen-types --db-url "postgresql://..." -s public -s auth
|
|
341
|
+
|
|
342
|
+
# PostgREST v9 compatibility
|
|
343
|
+
npx neon-js gen-types --db-url "postgresql://..." --postgrest-v9-compat
|
|
344
|
+
|
|
345
|
+
# Custom timeout
|
|
346
|
+
npx neon-js gen-types --db-url "postgresql://..." --query-timeout 30s
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
## Authentication Methods
|
|
350
|
+
|
|
351
|
+
The SDK supports the following authentication methods via the Stack Auth adapter:
|
|
352
|
+
|
|
353
|
+
### Fully Supported Methods
|
|
354
|
+
- **Email/Password**: `signUp()`, `signInWithPassword()`
|
|
355
|
+
- **OAuth**: `signInWithOAuth()` (supports all Stack Auth OAuth providers)
|
|
356
|
+
- **Magic Link**: `signInWithOtp()`, `verifyOtp()` (email-based passwordless authentication)
|
|
357
|
+
- **Session Management**: `getSession()`, `refreshSession()`, `setSession()`, `signOut()`
|
|
358
|
+
- **User Management**: `getUser()`, `updateUser()`, `getClaims()`, `getUserIdentities()`
|
|
359
|
+
- **Identity Linking**: `linkIdentity()`, `unlinkIdentity()`
|
|
360
|
+
- **Password Reset**: `resetPasswordForEmail()`, `resend()`
|
|
361
|
+
- **OAuth Callback**: `exchangeCodeForSession()`
|
|
362
|
+
- **State Monitoring**: `onAuthStateChange()`
|
|
363
|
+
|
|
364
|
+
### Unsupported Methods (Return Detailed Errors)
|
|
365
|
+
- **OIDC ID Token**: `signInWithIdToken()` - Stack Auth uses OAuth redirects only
|
|
366
|
+
- **SAML SSO**: `signInWithSSO()` - Stack Auth only supports OAuth social providers
|
|
367
|
+
- **Web3/Crypto**: `signInWithWeb3()` - Stack Auth does not support blockchain authentication
|
|
368
|
+
- **Anonymous**: `signInAnonymously()` - Use OAuth or email/password instead
|
|
369
|
+
|
|
370
|
+
For unsupported methods, the adapter returns comprehensive error messages explaining the limitation and suggesting alternatives.
|
|
371
|
+
|
|
372
|
+
## Performance
|
|
373
|
+
|
|
374
|
+
The Stack Auth adapter is optimized for performance:
|
|
375
|
+
|
|
376
|
+
- **Cached `getSession()`**: <5ms (reads from Stack Auth internal cache, no I/O)
|
|
377
|
+
- **First `getSession()` after reload**: <50ms (Stack Auth reads from tokenStore)
|
|
378
|
+
- **Token refresh**: <200ms (network call to Stack Auth, happens automatically)
|
|
379
|
+
|
|
380
|
+
## License
|
|
381
|
+
|
|
382
|
+
MIT
|
|
383
|
+
|
|
384
|
+
## Links
|
|
385
|
+
|
|
386
|
+
- [Stack Auth Documentation](https://docs.stack-auth.com/)
|
|
387
|
+
- [Supabase Auth Documentation](https://supabase.com/docs/guides/auth)
|
|
388
|
+
- [PostgrestClient Documentation](https://github.com/supabase/postgrest-js)
|