@synqauth/js 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +39 -2
- package/dist/api-Cf9K1Rc9.js +163 -0
- package/dist/index.d.ts +2055 -0
- package/dist/index.js +605 -0
- package/dist/management-y9-9AIc-.d.ts +11198 -0
- package/dist/server.d.ts +175 -0
- package/dist/server.js +197 -0
- package/package.json +46 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aerosol
|
|
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/README.md
CHANGED
|
@@ -1,3 +1,40 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @synqauth/js
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Synq's framework-agnostic client: sign-in and sessions in the browser, the account API, and token and webhook verification on your server.
|
|
4
|
+
|
|
5
|
+
Synq is customer identity for businesses: an OpenID Connect issuer for each of your brands, with Solana wallet, social, email and passkey sign-in. The documentation is at [docs.synq.build](https://docs.synq.build).
|
|
6
|
+
|
|
7
|
+
This package is at 0.x and hasn't been published yet: its API can change in any release until Synq's first customer depends on it.
|
|
8
|
+
|
|
9
|
+
## The account API
|
|
10
|
+
|
|
11
|
+
For an app that holds its own session, `createAccountClient` calls the brand's account API for the person signed in:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { accountApiUrl, createAccountClient, createSynqClient, linkSignIn } from '@synqauth/js'
|
|
15
|
+
|
|
16
|
+
const synq = createSynqClient({
|
|
17
|
+
issuer: 'https://acme.auth.synq.build',
|
|
18
|
+
clientId: 'app_…',
|
|
19
|
+
redirectUri: 'https://app.example.com/callback',
|
|
20
|
+
scope: 'openid offline_access me:read me:write',
|
|
21
|
+
resource: accountApiUrl('https://acme.auth.synq.build'),
|
|
22
|
+
})
|
|
23
|
+
const account = createAccountClient({
|
|
24
|
+
issuer: 'https://acme.auth.synq.build',
|
|
25
|
+
accessToken: () => synq.getAccessToken(),
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
const { data: me, error } = await account.me()
|
|
29
|
+
await account.user.update({ name: 'Ada Lovelace' })
|
|
30
|
+
|
|
31
|
+
// Adding a wallet: a link request, then a fresh sign-in that carries it.
|
|
32
|
+
const { data: request } = await account.accounts.link('wallet')
|
|
33
|
+
if (request) await synq.signIn(linkSignIn(request))
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Every call answers `{ data, error }` and never throws for what Synq answers; branch on `error.code`.
|
|
37
|
+
|
|
38
|
+
## License
|
|
39
|
+
|
|
40
|
+
MIT
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { AuthorizationResponseError, OperationProcessingError, ResponseBodyError, WWWAuthenticateChallengeError } from "oauth4webapi";
|
|
2
|
+
//#region src/errors.ts
|
|
3
|
+
/**
|
|
4
|
+
* Every error the SDK throws. Branch on `code`, which is stable: the API's
|
|
5
|
+
* problem `code`, OAuth's `error`, or one of the SDK's own; never on the
|
|
6
|
+
* message.
|
|
7
|
+
*/
|
|
8
|
+
var SynqError = class extends Error {
|
|
9
|
+
name = "SynqError";
|
|
10
|
+
code;
|
|
11
|
+
/** The HTTP status Synq answered with, when it answered. */
|
|
12
|
+
status;
|
|
13
|
+
/** What Synq said about it: OAuth's `error_description` or a problem's `detail`. */
|
|
14
|
+
description;
|
|
15
|
+
/**
|
|
16
|
+
* How many seconds to wait before trying again, from Synq's `Retry-After`:
|
|
17
|
+
* set on `rate_limited` (429), and wherever else Synq sends one.
|
|
18
|
+
*/
|
|
19
|
+
retryAfter;
|
|
20
|
+
constructor(code, message, options = {}) {
|
|
21
|
+
super(message, options.cause === void 0 ? void 0 : { cause: options.cause });
|
|
22
|
+
this.code = code;
|
|
23
|
+
this.status = options.status;
|
|
24
|
+
this.description = options.description;
|
|
25
|
+
this.retryAfter = options.retryAfter;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
/** Whether `error` is a `SynqError`, with `code` when given. */
|
|
29
|
+
function isSynqError(error, code) {
|
|
30
|
+
return error instanceof SynqError && (code === void 0 || error.code === code);
|
|
31
|
+
}
|
|
32
|
+
/** A `SynqError` for a problem document a Synq API answered with. */
|
|
33
|
+
function problemError(status, problem, retryAfter) {
|
|
34
|
+
const code = typeof problem.code === "string" ? problem.code : `http_${status}`;
|
|
35
|
+
const description = typeof problem.detail === "string" ? problem.detail : void 0;
|
|
36
|
+
return new SynqError(code, description ?? problem.title ?? `Synq answered ${status}`, {
|
|
37
|
+
status,
|
|
38
|
+
description,
|
|
39
|
+
retryAfter,
|
|
40
|
+
cause: problem
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The seconds a `Retry-After` header asks for: a number of seconds, or an
|
|
45
|
+
* HTTP date, counted from `now`. Undefined when there is none, or it can't
|
|
46
|
+
* be read.
|
|
47
|
+
*/
|
|
48
|
+
function retryAfterSeconds(header, now = Date.now()) {
|
|
49
|
+
const value = header?.trim();
|
|
50
|
+
if (!value) return void 0;
|
|
51
|
+
if (/^[0-9]+$/.test(value)) return Number(value);
|
|
52
|
+
if (!/[A-Za-z]/.test(value)) return void 0;
|
|
53
|
+
const at = Date.parse(value);
|
|
54
|
+
return Number.isNaN(at) ? void 0 : Math.max(0, Math.ceil((at - now) / 1e3));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* A `SynqError` for whatever went wrong talking to Synq's issuer: OAuth's
|
|
58
|
+
* error responses keep their `error`, a failed check of Synq's answer is
|
|
59
|
+
* `invalid_response`, and a request that never got an answer is
|
|
60
|
+
* `network_error`.
|
|
61
|
+
*/
|
|
62
|
+
function toSynqError(error) {
|
|
63
|
+
if (error instanceof SynqError) return error;
|
|
64
|
+
if (error instanceof ResponseBodyError) return new SynqError(error.error, error.error_description ?? error.error, {
|
|
65
|
+
status: error.status,
|
|
66
|
+
description: error.error_description,
|
|
67
|
+
cause: error
|
|
68
|
+
});
|
|
69
|
+
if (error instanceof AuthorizationResponseError) return new SynqError(error.error, error.error_description ?? error.error, {
|
|
70
|
+
description: error.error_description,
|
|
71
|
+
cause: error
|
|
72
|
+
});
|
|
73
|
+
if (error instanceof WWWAuthenticateChallengeError) {
|
|
74
|
+
const challenge = error.cause[0];
|
|
75
|
+
const code = challenge?.parameters.error ?? "invalid_token";
|
|
76
|
+
const description = challenge?.parameters.error_description;
|
|
77
|
+
return new SynqError(code, description ?? code, {
|
|
78
|
+
status: error.status,
|
|
79
|
+
description,
|
|
80
|
+
cause: error
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
if (error instanceof OperationProcessingError) return new SynqError("invalid_response", error.message, { cause: error });
|
|
84
|
+
if (error instanceof TypeError) return new SynqError("network_error", `Synq couldn't be reached: ${error.message}`, { cause: error });
|
|
85
|
+
return new SynqError("invalid_response", error instanceof Error ? error.message : "Synq answered with something unexpected", { cause: error });
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/urls.ts
|
|
89
|
+
/** Whether a host is this machine: a Synq stack under `localhost`, or a loopback address. */
|
|
90
|
+
function isLoopback(hostname) {
|
|
91
|
+
return hostname === "localhost" || hostname.endsWith(".localhost") || hostname === "127.0.0.1" || hostname === "[::1]";
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* `value` as a URL, which must be https: Synq's hosts always are. Plain http
|
|
95
|
+
* only on this machine, for a local Synq stack. `what` names it in the error.
|
|
96
|
+
*/
|
|
97
|
+
function secureUrl(value, what) {
|
|
98
|
+
let url;
|
|
99
|
+
try {
|
|
100
|
+
url = new URL(value);
|
|
101
|
+
} catch {
|
|
102
|
+
throw new SynqError("invalid_options", `${what} isn't a URL: ${String(value)}`);
|
|
103
|
+
}
|
|
104
|
+
if (url.protocol !== "https:" && !(url.protocol === "http:" && isLoopback(url.hostname))) throw new SynqError("invalid_options", `${what} must be https: (http: only for a Synq stack on this machine, under 'localhost')`);
|
|
105
|
+
return url;
|
|
106
|
+
}
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/api.ts
|
|
109
|
+
function createApiClient(options) {
|
|
110
|
+
const base = secureUrl(options.baseUrl, "The API base URL").href.replace(/\/+$/, "");
|
|
111
|
+
const call = (method) => ((path, request = {}) => send(method, path, request));
|
|
112
|
+
async function send(method, path, request) {
|
|
113
|
+
const url = new URL(base + fill(path, request.path));
|
|
114
|
+
for (const [name, value] of Object.entries(request.query ?? {})) {
|
|
115
|
+
if (value === void 0) continue;
|
|
116
|
+
for (const item of Array.isArray(value) ? value : [value]) url.searchParams.append(name, String(item));
|
|
117
|
+
}
|
|
118
|
+
const headers = new Headers({
|
|
119
|
+
accept: "application/json, application/problem+json",
|
|
120
|
+
authorization: `Bearer ${await options.credential()}`
|
|
121
|
+
});
|
|
122
|
+
if (request.body !== void 0) headers.set("content-type", "application/json");
|
|
123
|
+
if (request.idempotencyKey) headers.set("idempotency-key", request.idempotencyKey);
|
|
124
|
+
let response;
|
|
125
|
+
try {
|
|
126
|
+
response = await (options.fetch ?? fetch)(url, {
|
|
127
|
+
method: method.toUpperCase(),
|
|
128
|
+
headers,
|
|
129
|
+
body: request.body === void 0 ? void 0 : JSON.stringify(request.body),
|
|
130
|
+
signal: request.signal
|
|
131
|
+
});
|
|
132
|
+
} catch (error) {
|
|
133
|
+
throw new SynqError("network_error", `Synq couldn't be reached: ${String(error)}`, { cause: error });
|
|
134
|
+
}
|
|
135
|
+
const text = await response.text();
|
|
136
|
+
let json;
|
|
137
|
+
try {
|
|
138
|
+
json = text ? JSON.parse(text) : void 0;
|
|
139
|
+
} catch {
|
|
140
|
+
json = void 0;
|
|
141
|
+
}
|
|
142
|
+
if (!response.ok) throw problemError(response.status, json !== null && typeof json === "object" ? json : {}, retryAfterSeconds(response.headers.get("retry-after")));
|
|
143
|
+
return json;
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
get: call("get"),
|
|
147
|
+
post: call("post"),
|
|
148
|
+
put: call("put"),
|
|
149
|
+
patch: call("patch"),
|
|
150
|
+
delete: call("delete")
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/** The path with each `{name}` replaced by its parameter, encoded. */
|
|
154
|
+
function fill(path, params = {}) {
|
|
155
|
+
return path.replace(/\{([^}]+)\}/g, (_, name) => {
|
|
156
|
+
const value = params[name];
|
|
157
|
+
if (value === void 0 || value === "") throw new SynqError("invalid_options", `The path ${path} needs ${name}`);
|
|
158
|
+
if (/^\.+$/.test(String(value))) throw new SynqError("invalid_options", `${name} can't be ${String(value)}`);
|
|
159
|
+
return encodeURIComponent(String(value));
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
//#endregion
|
|
163
|
+
export { problemError as a, isSynqError as i, secureUrl as n, toSynqError as o, SynqError as r, createApiClient as t };
|