@zyphr-dev/node-sdk 0.1.45 → 0.1.46
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/dist/index.cjs +21 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +20 -5
- package/dist/index.d.ts +20 -5
- package/dist/index.js +21 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/clientAuth.ts +34 -8
- package/src/src/models/WebhookEventType.ts +1 -0
package/package.json
CHANGED
package/src/clientAuth.ts
CHANGED
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
* const login = await zyphr.auth.login.loginEndUser({ ... });
|
|
29
29
|
* zyphr.setAccessToken(login.data.tokens.access_token);
|
|
30
30
|
*
|
|
31
|
-
*
|
|
31
|
+
* // The stored token is applied automatically to user-scoped calls:
|
|
32
|
+
* const me = await zyphr.auth.profile.getEndUser();
|
|
32
33
|
* ```
|
|
33
34
|
*/
|
|
34
35
|
|
|
@@ -60,7 +61,14 @@ export interface ZyphrClientOptions {
|
|
|
60
61
|
applicationKey: string;
|
|
61
62
|
/** Override the base API URL (defaults to https://api.zyphr.dev/v1). */
|
|
62
63
|
baseUrl?: string;
|
|
63
|
-
/**
|
|
64
|
+
/**
|
|
65
|
+
* Optional end-user access token, if you already have a session on
|
|
66
|
+
* construction. Once set (here or via {@link ZyphrClient.setAccessToken}), it
|
|
67
|
+
* is applied automatically as `Authorization: Bearer` to every user-scoped
|
|
68
|
+
* call — you no longer need to pass {@link ZyphrClient.authedRequest} for
|
|
69
|
+
* authentication (though authedRequest still works and a token passed to it
|
|
70
|
+
* overrides the stored one).
|
|
71
|
+
*/
|
|
64
72
|
accessToken?: string;
|
|
65
73
|
}
|
|
66
74
|
|
|
@@ -157,10 +165,18 @@ export class ZyphrClient {
|
|
|
157
165
|
|
|
158
166
|
const config = new Configuration({
|
|
159
167
|
basePath: options.baseUrl,
|
|
160
|
-
// The
|
|
161
|
-
// application key. End-user Bearer tokens are attached per-request via
|
|
162
|
-
// authedRequest() so token rotation doesn't require rebuilding the client.
|
|
168
|
+
// The publishable application key is sent automatically on every call.
|
|
163
169
|
apiKey: (name: string) => (name === 'X-Application-Key' ? this.applicationKey : ''),
|
|
170
|
+
// The stored end-user access token is also applied automatically: any
|
|
171
|
+
// user-scoped generated method (which authenticates the EndUserToken
|
|
172
|
+
// scheme) invokes this callback and attaches `Authorization: Bearer`.
|
|
173
|
+
// The callback reads `this.accessToken` at call time, so setAccessToken()
|
|
174
|
+
// takes effect on subsequent calls without rebuilding the client. When no
|
|
175
|
+
// token is set it returns '', and the generated code adds no header — so
|
|
176
|
+
// anonymous endpoints (login, register, password-reset) stay unauthenticated.
|
|
177
|
+
// An explicit token passed to authedRequest() sets the header directly in
|
|
178
|
+
// RequestInit, which overrides this callback.
|
|
179
|
+
accessToken: async () => this.accessToken ?? '',
|
|
164
180
|
middleware: [errorMiddleware],
|
|
165
181
|
});
|
|
166
182
|
|
|
@@ -182,7 +198,7 @@ export class ZyphrClient {
|
|
|
182
198
|
this.devices = new DevicesApi(config);
|
|
183
199
|
}
|
|
184
200
|
|
|
185
|
-
/** Store the end-user access token
|
|
201
|
+
/** Store the end-user access token; it is applied automatically to subsequent user-scoped calls. */
|
|
186
202
|
setAccessToken(accessToken: string | undefined): void {
|
|
187
203
|
this.accessToken = accessToken;
|
|
188
204
|
}
|
|
@@ -195,15 +211,25 @@ export class ZyphrClient {
|
|
|
195
211
|
/**
|
|
196
212
|
* Build request overrides that authenticate as the current end user: the
|
|
197
213
|
* stored Bearer token plus the publishable application key required by
|
|
198
|
-
* user-scoped endpoints.
|
|
214
|
+
* user-scoped endpoints.
|
|
215
|
+
*
|
|
216
|
+
* The stored token is now applied automatically to user-scoped calls, so this
|
|
217
|
+
* is no longer required for authentication. It remains useful to authenticate
|
|
218
|
+
* a single call with a different token — a token passed here sets the header
|
|
219
|
+
* directly in RequestInit and overrides the stored token for that call.
|
|
199
220
|
*
|
|
200
221
|
* @example
|
|
201
222
|
* ```ts
|
|
202
|
-
*
|
|
223
|
+
* // Authenticate one call as a different user, without changing stored state:
|
|
224
|
+
* const me = await zyphr.auth.profile.getEndUser(zyphr.authedRequest(otherToken));
|
|
203
225
|
* ```
|
|
204
226
|
*/
|
|
205
227
|
authedRequest(accessToken?: string): RequestInit {
|
|
206
228
|
const token = accessToken ?? this.accessToken;
|
|
229
|
+
// Must include X-Application-Key: the runtime replaces the whole headers
|
|
230
|
+
// object with this initOverride (it does not deep-merge), so anything not
|
|
231
|
+
// re-listed here — including the app key the Configuration would otherwise
|
|
232
|
+
// add — is dropped for this call.
|
|
207
233
|
const headers: Record<string, string> = {
|
|
208
234
|
'X-Application-Key': this.applicationKey,
|
|
209
235
|
};
|
|
@@ -27,6 +27,7 @@ export const WebhookEventType = {
|
|
|
27
27
|
EMAIL_OPENED: 'email.opened',
|
|
28
28
|
EMAIL_CLICKED: 'email.clicked',
|
|
29
29
|
EMAIL_FAILED: 'email.failed',
|
|
30
|
+
EMAIL_SUPPRESSED: 'email.suppressed',
|
|
30
31
|
EMAIL_RECEIVED: 'email.received',
|
|
31
32
|
PUSH_SENT: 'push.sent',
|
|
32
33
|
PUSH_DELIVERED: 'push.delivered',
|