@zyphr-dev/node-sdk 0.1.10 → 0.1.14

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zyphr-dev/node-sdk",
3
- "version": "0.1.10",
3
+ "version": "0.1.14",
4
4
  "description": "Official Zyphr SDK for Node.js, React, and React Native",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
package/src/client.ts CHANGED
@@ -50,6 +50,10 @@ export interface ZyphrOptions {
50
50
  apiKey: string;
51
51
  /** Override the base API URL (defaults to https://api.zyphr.dev/v1) */
52
52
  baseUrl?: string;
53
+ /** Application public key (za_pub_*) for Auth-as-a-Service endpoints */
54
+ applicationKey?: string;
55
+ /** Application secret key (za_sec_*) for Auth-as-a-Service endpoints */
56
+ applicationSecret?: string;
53
57
  }
54
58
 
55
59
  /**
@@ -102,7 +106,12 @@ export class Zyphr {
102
106
  constructor(options: ZyphrOptions) {
103
107
  const config = new Configuration({
104
108
  basePath: options.baseUrl,
105
- apiKey: () => options.apiKey,
109
+ apiKey: (name: string) => {
110
+ // Route application credential headers to the right keys
111
+ if (name === 'X-Application-Key' && options.applicationKey) return options.applicationKey;
112
+ if (name === 'X-Application-Secret' && options.applicationSecret) return options.applicationSecret;
113
+ return options.apiKey;
114
+ },
106
115
  middleware: [errorMiddleware],
107
116
  });
108
117
 
@@ -139,4 +148,25 @@ export class Zyphr {
139
148
  webauthn: new AuthWebAuthnApi(config),
140
149
  };
141
150
  }
151
+
152
+ /**
153
+ * Create request overrides that authenticate as an end user.
154
+ * Use this when calling profile, session, or MFA endpoints that
155
+ * require the end user's access token instead of the API key.
156
+ *
157
+ * @example
158
+ * ```ts
159
+ * const login = await zyphr.auth.login.loginEndUser({ ... });
160
+ * const token = login.data.tokens.access_token;
161
+ *
162
+ * const profile = await zyphr.auth.profile.getEndUser(
163
+ * zyphr.asEndUser(token)
164
+ * );
165
+ * ```
166
+ */
167
+ asEndUser(accessToken: string): RequestInit {
168
+ return {
169
+ headers: { Authorization: `Bearer ${accessToken}` },
170
+ };
171
+ }
142
172
  }