@c9up/aurora 0.1.6 → 0.1.7
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/README.md +1 -1
- package/dist/browser.d.ts +136 -4
- package/dist/browser.js +324 -15
- package/dist/http.d.ts +79 -0
- package/dist/http.js +199 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +2 -7
- package/package.json +1 -1
- package/src/browser.ts +425 -16
- package/src/http.ts +278 -0
- package/src/index.ts +32 -1
package/dist/http.js
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `HttpClient` — a small typed wrapper over `fetch` so call sites read
|
|
3
|
+
* `await http.get<User>("/auth/me")` instead of hand-rolling headers,
|
|
4
|
+
* `res.json()`, and status checks.
|
|
5
|
+
*
|
|
6
|
+
* - Auto JSON: a plain-object/array body is `JSON.stringify`-d with a
|
|
7
|
+
* `Content-Type: application/json` header; a JSON response is parsed.
|
|
8
|
+
* `FormData`/`Blob`/`URLSearchParams`/`string`/binary bodies pass through
|
|
9
|
+
* untouched.
|
|
10
|
+
* - Bearer auth: a `token` (string or getter, read fresh per request) is sent
|
|
11
|
+
* as `Authorization: Bearer …` unless the caller set the header themselves.
|
|
12
|
+
* - Errors: a non-2xx response rejects with an {@link HttpError} carrying the
|
|
13
|
+
* status, the `Response`, and the parsed body.
|
|
14
|
+
*
|
|
15
|
+
* Node-free and isomorphic — uses the global `fetch` (browsers, Node 18+,
|
|
16
|
+
* Workers, Bun, Deno). Part of the client barrel.
|
|
17
|
+
*/
|
|
18
|
+
/** Thrown on a non-2xx response. Carries the status, the `Response`, and the parsed body. */
|
|
19
|
+
export class HttpError extends Error {
|
|
20
|
+
status;
|
|
21
|
+
response;
|
|
22
|
+
data;
|
|
23
|
+
constructor(response, data) {
|
|
24
|
+
super(`HTTP ${response.status} ${response.statusText} for ${response.url}`);
|
|
25
|
+
this.name = "HttpError";
|
|
26
|
+
this.status = response.status;
|
|
27
|
+
this.response = response;
|
|
28
|
+
this.data = data;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** Whether `body` should be JSON-encoded (vs. passed to `fetch` untouched). */
|
|
32
|
+
function shouldJsonEncode(body) {
|
|
33
|
+
if (body === null || typeof body !== "object") {
|
|
34
|
+
return typeof body !== "string";
|
|
35
|
+
}
|
|
36
|
+
if (body instanceof FormData ||
|
|
37
|
+
body instanceof Blob ||
|
|
38
|
+
body instanceof URLSearchParams ||
|
|
39
|
+
body instanceof ArrayBuffer ||
|
|
40
|
+
ArrayBuffer.isView(body)) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
if (typeof ReadableStream !== "undefined" && body instanceof ReadableStream) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
/** Case-insensitive header presence check. */
|
|
49
|
+
function hasHeader(headers, name) {
|
|
50
|
+
const lower = name.toLowerCase();
|
|
51
|
+
for (const key of Object.keys(headers)) {
|
|
52
|
+
if (key.toLowerCase() === lower)
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
/** Parse a response by content-type; `null` for empty / no-content bodies. */
|
|
58
|
+
async function parseBody(response) {
|
|
59
|
+
if (response.status === 204 || response.status === 205)
|
|
60
|
+
return null;
|
|
61
|
+
const type = response.headers.get("content-type") ?? "";
|
|
62
|
+
const text = await response.text();
|
|
63
|
+
if (text === "")
|
|
64
|
+
return null;
|
|
65
|
+
if (type.includes("application/json"))
|
|
66
|
+
return JSON.parse(text);
|
|
67
|
+
return text;
|
|
68
|
+
}
|
|
69
|
+
export class HttpClient {
|
|
70
|
+
#baseURL;
|
|
71
|
+
#headers;
|
|
72
|
+
#token;
|
|
73
|
+
#credentials;
|
|
74
|
+
constructor(options = {}) {
|
|
75
|
+
this.#baseURL = options.baseURL ?? "";
|
|
76
|
+
this.#headers = { ...options.headers };
|
|
77
|
+
this.#token = options.token;
|
|
78
|
+
this.#credentials = options.credentials;
|
|
79
|
+
}
|
|
80
|
+
/** Set a default header for every subsequent request (case-insensitive replace). Chainable. */
|
|
81
|
+
setHeader(name, value) {
|
|
82
|
+
this.#deleteHeader(name);
|
|
83
|
+
this.#headers[name] = value;
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
/** Merge several default headers at once. Chainable. */
|
|
87
|
+
setHeaders(headers) {
|
|
88
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
89
|
+
this.setHeader(name, value);
|
|
90
|
+
}
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
/** Remove a default header (case-insensitive). Chainable. */
|
|
94
|
+
removeHeader(name) {
|
|
95
|
+
this.#deleteHeader(name);
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
/** A copy of the current default headers. */
|
|
99
|
+
getHeaders() {
|
|
100
|
+
return { ...this.#headers };
|
|
101
|
+
}
|
|
102
|
+
#deleteHeader(name) {
|
|
103
|
+
const lower = name.toLowerCase();
|
|
104
|
+
for (const key of Object.keys(this.#headers)) {
|
|
105
|
+
if (key.toLowerCase() === lower)
|
|
106
|
+
delete this.#headers[key];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
get(url, options) {
|
|
110
|
+
return this.#request("GET", url, undefined, options);
|
|
111
|
+
}
|
|
112
|
+
delete(url, options) {
|
|
113
|
+
return this.#request("DELETE", url, undefined, options);
|
|
114
|
+
}
|
|
115
|
+
post(url, body, options) {
|
|
116
|
+
return this.#request("POST", url, body, options);
|
|
117
|
+
}
|
|
118
|
+
put(url, body, options) {
|
|
119
|
+
return this.#request("PUT", url, body, options);
|
|
120
|
+
}
|
|
121
|
+
patch(url, body, options) {
|
|
122
|
+
return this.#request("PATCH", url, body, options);
|
|
123
|
+
}
|
|
124
|
+
/** Send a request and return the raw `Response` (no parsing, no throw on non-2xx). */
|
|
125
|
+
raw(method, url, body, options = {}) {
|
|
126
|
+
return this.#send(method, url, body, options);
|
|
127
|
+
}
|
|
128
|
+
/** Derive a new client with merged defaults (e.g. a scope that adds a token). */
|
|
129
|
+
extend(options) {
|
|
130
|
+
return new HttpClient({
|
|
131
|
+
baseURL: options.baseURL ?? this.#baseURL,
|
|
132
|
+
headers: { ...this.#headers, ...options.headers },
|
|
133
|
+
token: options.token ?? this.#token,
|
|
134
|
+
credentials: options.credentials ?? this.#credentials,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
#resolveToken(override) {
|
|
138
|
+
if (override !== undefined)
|
|
139
|
+
return override;
|
|
140
|
+
return typeof this.#token === "function" ? this.#token() : this.#token;
|
|
141
|
+
}
|
|
142
|
+
#buildUrl(url, query) {
|
|
143
|
+
const base = /^[a-z][a-z\d+\-.]*:\/\//i.test(url)
|
|
144
|
+
? url
|
|
145
|
+
: this.#baseURL + url;
|
|
146
|
+
if (!query)
|
|
147
|
+
return base;
|
|
148
|
+
const params = new URLSearchParams();
|
|
149
|
+
for (const [key, value] of Object.entries(query)) {
|
|
150
|
+
if (value !== null && value !== undefined)
|
|
151
|
+
params.append(key, String(value));
|
|
152
|
+
}
|
|
153
|
+
const qs = params.toString();
|
|
154
|
+
if (qs === "")
|
|
155
|
+
return base;
|
|
156
|
+
return `${base}${base.includes("?") ? "&" : "?"}${qs}`;
|
|
157
|
+
}
|
|
158
|
+
#send(method, url, body, options) {
|
|
159
|
+
const headers = {
|
|
160
|
+
...this.#headers,
|
|
161
|
+
...options.headers,
|
|
162
|
+
};
|
|
163
|
+
const token = this.#resolveToken(options.token);
|
|
164
|
+
if (token != null && !hasHeader(headers, "authorization")) {
|
|
165
|
+
headers.Authorization = `Bearer ${token}`;
|
|
166
|
+
}
|
|
167
|
+
let payload;
|
|
168
|
+
if (body !== undefined && body !== null) {
|
|
169
|
+
if (shouldJsonEncode(body)) {
|
|
170
|
+
payload = JSON.stringify(body);
|
|
171
|
+
if (!hasHeader(headers, "content-type")) {
|
|
172
|
+
headers["Content-Type"] = "application/json";
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
// Already a valid BodyInit (string / FormData / Blob / …).
|
|
177
|
+
payload = body;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return fetch(this.#buildUrl(url, options.query), {
|
|
181
|
+
method,
|
|
182
|
+
headers,
|
|
183
|
+
body: payload,
|
|
184
|
+
signal: options.signal,
|
|
185
|
+
credentials: options.credentials ?? this.#credentials,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
async #request(method, url, body, options = {}) {
|
|
189
|
+
const response = await this.#send(method, url, body, options);
|
|
190
|
+
const data = await parseBody(response);
|
|
191
|
+
if (!response.ok)
|
|
192
|
+
throw new HttpError(response, data);
|
|
193
|
+
// `parse` validates at runtime; without it, `T` is the caller's
|
|
194
|
+
// unchecked assertion of the response shape (the usual HTTP boundary).
|
|
195
|
+
return options.parse ? options.parse(data) : data;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/** Default same-origin client. Configure your own via `new HttpClient({ … })`. */
|
|
199
|
+
export const http = new HttpClient();
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export type { CookieOptions, PersistedSignalOptions, ShareData, StorageArea, WebStorageOptions, WindowSize, } from "./browser.js";
|
|
2
|
+
export { back, clipboard, cookie, forward, hash, mediaQuery, navigate, online, persistedSignal, prefersDark, queryParam, redirect, reload, replace, session, share, storage, visibility, WebStorage, windowSize, } from "./browser.js";
|
|
2
3
|
export { component, onMount, onUnmount } from "./component.js";
|
|
3
4
|
export { html, isTemplateResult } from "./html.js";
|
|
5
|
+
export type { HttpClientOptions, HttpRequestOptions } from "./http.js";
|
|
6
|
+
export { HttpClient, HttpError, http } from "./http.js";
|
|
4
7
|
export { hydrate } from "./hydrate.js";
|
|
5
8
|
export { batch, effect, isSignal, memo, onCleanup, type ReadSignal, type Signal, signal, untrack, } from "./reactive.js";
|
|
6
9
|
export { type Disposer, render } from "./render.js";
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
//
|
|
3
|
-
// Server-only exports (AuroraManager, Pages, renderPage, serveAssets) that pull
|
|
4
|
-
// node:fs / node:path / node:url live in `@c9up/aurora/server`. Keeping them off
|
|
5
|
-
// this barrel is what lets a browser bundle import the client primitives without
|
|
6
|
-
// the bundler dragging Node built-ins through the import graph.
|
|
7
|
-
export { redirect, reload, replace, storage } from "./browser.js";
|
|
1
|
+
export { back, clipboard, cookie, forward, hash, mediaQuery, navigate, online, persistedSignal, prefersDark, queryParam, redirect, reload, replace, session, share, storage, visibility, WebStorage, windowSize, } from "./browser.js";
|
|
8
2
|
export { component, onMount, onUnmount } from "./component.js";
|
|
9
3
|
export { html, isTemplateResult } from "./html.js";
|
|
4
|
+
export { HttpClient, HttpError, http } from "./http.js";
|
|
10
5
|
export { hydrate } from "./hydrate.js";
|
|
11
6
|
export { batch, effect, isSignal, memo, onCleanup, signal, untrack, } from "./reactive.js";
|
|
12
7
|
export { render } from "./render.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@c9up/aurora",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Aurora — reactive UI runtime for the Ream framework. Tagged-template DOM, signal-based state, isomorphic SSR + hydration, zero build step.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|