@neon-pixels/feedback-client 0.0.0 → 0.3.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 +54 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.js +165 -0
- package/package.json +32 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Neon Pixels Studio
|
|
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
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# @neon-pixels/feedback-client
|
|
2
|
+
|
|
3
|
+
Sends user feedback from your server to the Neon Pixels dashboard's feedback endpoint. One request, one typed error, and the receiver's field limits so a route can answer 400 before sending. Your app's own feedback route, its auth, and its throttle stay in your app.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @neon-pixels/feedback-client
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
There are no runtime dependencies, and nothing reads `process.env`: pass the token in.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
The token is a dashboard API key. Keep it on the server; never send it to a browser.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import {
|
|
19
|
+
FeedbackError,
|
|
20
|
+
sendFeedback,
|
|
21
|
+
validateFeedback,
|
|
22
|
+
} from "@neon-pixels/feedback-client";
|
|
23
|
+
|
|
24
|
+
const problems = validateFeedback({ message, title, email, url });
|
|
25
|
+
if (problems.length > 0) {
|
|
26
|
+
return respond(400, problems);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
await sendFeedback({ token, message, title, email, url });
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if (error instanceof FeedbackError) {
|
|
33
|
+
// error.reason: "config" | "invalid" | "http" | "timeout" | "network"
|
|
34
|
+
// error.status: the HTTP status, when the dashboard answered
|
|
35
|
+
}
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### `sendFeedback(input, options?)`
|
|
41
|
+
|
|
42
|
+
Sends a `POST` with `Authorization: Bearer <token>` and a JSON body. `message` is required; `title`, `email`, and `url` are left out of the body when empty after trimming. A missing token, a non-https `endpoint` (plain http is allowed for localhost only), or an unusable `timeoutMs` throws a `FeedbackError` with reason `"config"` before any request is made. Input that `validateFeedback` would reject throws with reason `"invalid"`. Redirects are not followed.
|
|
43
|
+
|
|
44
|
+
| Option | Default | Meaning |
|
|
45
|
+
| ----------- | --------------------------------- | ----------------------------------------------- |
|
|
46
|
+
| `fetch` | global `fetch` | Inject your own, for tests. |
|
|
47
|
+
| `endpoint` | `DEFAULT_FEEDBACK_ENDPOINT` | `https://dashboard.neonpixels.dev/api/feedback` |
|
|
48
|
+
| `timeoutMs` | `DEFAULT_TIMEOUT_MS` (10 seconds) | Aborts the request when it runs out. |
|
|
49
|
+
|
|
50
|
+
A non-2xx answer, a timeout, and a network failure each throw a `FeedbackError`. Its message never contains the token.
|
|
51
|
+
|
|
52
|
+
### `validateFeedback(input)` and `FEEDBACK_LIMITS`
|
|
53
|
+
|
|
54
|
+
Returns a list of `{ field, message }` problems and never throws. It checks that `message` is a non-empty string and that each field is within `FEEDBACK_LIMITS` after trimming: message 10000, title 120, email 254, url 2000 characters. These mirror the receiver; if the receiver changes them, this package changes with it.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
type FeedbackErrorReason = "config" | "invalid" | "http" | "timeout" | "network";
|
|
2
|
+
/** The one error `sendFeedback` throws. Its message never contains the token. */
|
|
3
|
+
export declare class FeedbackError extends Error {
|
|
4
|
+
readonly reason: FeedbackErrorReason;
|
|
5
|
+
/** The HTTP status, when the dashboard answered. */
|
|
6
|
+
readonly status: number | undefined;
|
|
7
|
+
constructor(reason: FeedbackErrorReason, message: string, status?: number);
|
|
8
|
+
}
|
|
9
|
+
interface FeedbackInput {
|
|
10
|
+
message: string;
|
|
11
|
+
title?: string;
|
|
12
|
+
email?: string;
|
|
13
|
+
url?: string;
|
|
14
|
+
}
|
|
15
|
+
interface SendFeedbackInput extends FeedbackInput {
|
|
16
|
+
/** Dashboard API key. Server-only: never reaches a browser. */
|
|
17
|
+
token: string;
|
|
18
|
+
}
|
|
19
|
+
type FetchLike = (input: string, init: RequestInit) => Promise<Response>;
|
|
20
|
+
interface SendFeedbackOptions {
|
|
21
|
+
/** Defaults to the global `fetch`. */
|
|
22
|
+
fetch?: FetchLike;
|
|
23
|
+
/** Defaults to `DEFAULT_FEEDBACK_ENDPOINT`. */
|
|
24
|
+
endpoint?: string;
|
|
25
|
+
/** Aborts the request after this long. Defaults to 10 seconds. */
|
|
26
|
+
timeoutMs?: number;
|
|
27
|
+
}
|
|
28
|
+
interface FeedbackProblem {
|
|
29
|
+
field: keyof FeedbackInput;
|
|
30
|
+
message: string;
|
|
31
|
+
}
|
|
32
|
+
/** The receiver's field limits, in characters after trimming. */
|
|
33
|
+
export declare const FEEDBACK_LIMITS: {
|
|
34
|
+
readonly message: 10000;
|
|
35
|
+
readonly title: 120;
|
|
36
|
+
readonly email: 254;
|
|
37
|
+
readonly url: 2000;
|
|
38
|
+
};
|
|
39
|
+
/** Lists what the receiver would reject. Never throws, whatever the input. */
|
|
40
|
+
export declare function validateFeedback(input: unknown): FeedbackProblem[];
|
|
41
|
+
export declare const DEFAULT_FEEDBACK_ENDPOINT = "https://dashboard.neonpixels.dev/api/feedback";
|
|
42
|
+
export declare const DEFAULT_TIMEOUT_MS = 10000;
|
|
43
|
+
/**
|
|
44
|
+
* Sends one feedback submission to the dashboard. Throws `FeedbackError` for
|
|
45
|
+
* a missing token or invalid input (before any request), a non-2xx answer, a
|
|
46
|
+
* timeout, or a network failure.
|
|
47
|
+
*/
|
|
48
|
+
export declare function sendFeedback({ token, ...feedback }: SendFeedbackInput, options?: SendFeedbackOptions): Promise<void>;
|
|
49
|
+
export type { FeedbackErrorReason, FeedbackInput, FeedbackProblem, FetchLike, SendFeedbackInput, SendFeedbackOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// src/error.ts
|
|
2
|
+
/** The one error `sendFeedback` throws. Its message never contains the token. */
|
|
3
|
+
var FeedbackError = class extends Error {
|
|
4
|
+
reason;
|
|
5
|
+
/** The HTTP status, when the dashboard answered. */
|
|
6
|
+
status;
|
|
7
|
+
constructor(reason, message, status) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "FeedbackError";
|
|
10
|
+
this.reason = reason;
|
|
11
|
+
this.status = status;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
// src/types.ts
|
|
15
|
+
const OPTIONAL_FIELDS = [
|
|
16
|
+
"title",
|
|
17
|
+
"email",
|
|
18
|
+
"url"
|
|
19
|
+
];
|
|
20
|
+
// src/validate.ts
|
|
21
|
+
/** The receiver's field limits, in characters after trimming. */
|
|
22
|
+
const FEEDBACK_LIMITS = {
|
|
23
|
+
message: 1e4,
|
|
24
|
+
title: 120,
|
|
25
|
+
email: 254,
|
|
26
|
+
url: 2e3
|
|
27
|
+
};
|
|
28
|
+
function checkLength(field, value) {
|
|
29
|
+
if (value.trim().length <= FEEDBACK_LIMITS[field]) return;
|
|
30
|
+
return {
|
|
31
|
+
field,
|
|
32
|
+
message: `${field} must be at most ${FEEDBACK_LIMITS[field]} characters.`
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function checkMessage(value) {
|
|
36
|
+
if (typeof value !== "string" || value.trim() === "") return {
|
|
37
|
+
field: "message",
|
|
38
|
+
message: "message must be a non-empty string."
|
|
39
|
+
};
|
|
40
|
+
return checkLength("message", value);
|
|
41
|
+
}
|
|
42
|
+
function checkOptional(field, value) {
|
|
43
|
+
if (value === void 0 || value === null) return;
|
|
44
|
+
if (typeof value !== "string") return {
|
|
45
|
+
field,
|
|
46
|
+
message: `${field} must be a string.`
|
|
47
|
+
};
|
|
48
|
+
return checkLength(field, value);
|
|
49
|
+
}
|
|
50
|
+
/** Lists what the receiver would reject. Never throws, whatever the input. */
|
|
51
|
+
function validateFeedback(input) {
|
|
52
|
+
const source = typeof input === "object" && input !== null ? input : {};
|
|
53
|
+
return [checkMessage(source.message), ...OPTIONAL_FIELDS.map((field) => checkOptional(field, source[field]))].filter((problem) => problem !== void 0);
|
|
54
|
+
}
|
|
55
|
+
// src/send.ts
|
|
56
|
+
const DEFAULT_FEEDBACK_ENDPOINT = "https://dashboard.neonpixels.dev/api/feedback";
|
|
57
|
+
const DEFAULT_TIMEOUT_MS = 1e4;
|
|
58
|
+
const MAX_TIMEOUT_MS = 0x7fffffff;
|
|
59
|
+
function buildBody(input) {
|
|
60
|
+
const optionalEntries = OPTIONAL_FIELDS.map((field) => [field, input[field]?.trim()]).filter(([, value]) => Boolean(value));
|
|
61
|
+
return {
|
|
62
|
+
message: input.message.trim(),
|
|
63
|
+
...Object.fromEntries(optionalEntries)
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
const HEADER_SAFE_TOKEN = /^[\x21-\x7E]+$/;
|
|
67
|
+
function requireToken(token) {
|
|
68
|
+
const trimmed = typeof token === "string" ? token.trim() : "";
|
|
69
|
+
if (!HEADER_SAFE_TOKEN.test(trimmed)) throw new FeedbackError("config", "Feedback token is missing or malformed.");
|
|
70
|
+
return trimmed;
|
|
71
|
+
}
|
|
72
|
+
function requireTimeout(timeoutMs) {
|
|
73
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs < 1 || timeoutMs > MAX_TIMEOUT_MS) throw new FeedbackError("config", `timeoutMs must be between 1 and ${MAX_TIMEOUT_MS}.`);
|
|
74
|
+
return timeoutMs;
|
|
75
|
+
}
|
|
76
|
+
const LOCAL_HOSTS = /* @__PURE__ */ new Set([
|
|
77
|
+
"localhost",
|
|
78
|
+
"127.0.0.1",
|
|
79
|
+
"[::1]"
|
|
80
|
+
]);
|
|
81
|
+
function isSafeEndpoint(endpoint) {
|
|
82
|
+
try {
|
|
83
|
+
const { protocol, hostname } = new URL(endpoint);
|
|
84
|
+
return protocol === "https:" || protocol === "http:" && LOCAL_HOSTS.has(hostname);
|
|
85
|
+
} catch {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function requireEndpoint(endpoint) {
|
|
90
|
+
if (!isSafeEndpoint(endpoint)) throw new FeedbackError("config", "endpoint must be an https URL.");
|
|
91
|
+
return endpoint;
|
|
92
|
+
}
|
|
93
|
+
function requireValidFeedback(feedback) {
|
|
94
|
+
const problems = validateFeedback(feedback);
|
|
95
|
+
if (problems.length === 0) return;
|
|
96
|
+
throw new FeedbackError("invalid", `Invalid feedback. ${problems.map((problem) => problem.message).join(" ")}`);
|
|
97
|
+
}
|
|
98
|
+
async function post(fetchFunction, endpoint, token, body, signal) {
|
|
99
|
+
return fetchFunction(endpoint, {
|
|
100
|
+
method: "POST",
|
|
101
|
+
headers: {
|
|
102
|
+
Authorization: `Bearer ${token}`,
|
|
103
|
+
"Content-Type": "application/json"
|
|
104
|
+
},
|
|
105
|
+
body: JSON.stringify(body),
|
|
106
|
+
redirect: "manual",
|
|
107
|
+
signal
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
async function discardBody(response) {
|
|
111
|
+
await response.body?.cancel().catch(() => void 0);
|
|
112
|
+
}
|
|
113
|
+
function assertOk(response) {
|
|
114
|
+
if (response.ok) return;
|
|
115
|
+
throw new FeedbackError("http", `Feedback submission failed (${response.status}).`, response.status);
|
|
116
|
+
}
|
|
117
|
+
function startTimeout(controller, timeoutMs) {
|
|
118
|
+
let timer;
|
|
119
|
+
return {
|
|
120
|
+
expired: new Promise((_resolve, reject) => {
|
|
121
|
+
timer = setTimeout(() => {
|
|
122
|
+
controller.abort();
|
|
123
|
+
reject(timeoutError(timeoutMs));
|
|
124
|
+
}, timeoutMs);
|
|
125
|
+
}),
|
|
126
|
+
cancel: () => clearTimeout(timer)
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function timeoutError(timeoutMs) {
|
|
130
|
+
return new FeedbackError("timeout", `Feedback submission timed out after ${timeoutMs}ms.`);
|
|
131
|
+
}
|
|
132
|
+
function networkCode(error) {
|
|
133
|
+
const code = error?.cause?.code;
|
|
134
|
+
return typeof code === "string" && /^[A-Z0-9_]+$/.test(code) ? `: ${code}` : "";
|
|
135
|
+
}
|
|
136
|
+
function toFeedbackError(error, controller, timeoutMs) {
|
|
137
|
+
if (error instanceof FeedbackError) return error;
|
|
138
|
+
if (controller.signal.aborted) return timeoutError(timeoutMs);
|
|
139
|
+
return new FeedbackError("network", `Feedback submission failed (network${networkCode(error)}).`);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Sends one feedback submission to the dashboard. Throws `FeedbackError` for
|
|
143
|
+
* a missing token or invalid input (before any request), a non-2xx answer, a
|
|
144
|
+
* timeout, or a network failure.
|
|
145
|
+
*/
|
|
146
|
+
async function sendFeedback({ token, ...feedback }, options = {}) {
|
|
147
|
+
const bearer = requireToken(token);
|
|
148
|
+
const { fetch: fetchFunction = fetch, endpoint = DEFAULT_FEEDBACK_ENDPOINT, timeoutMs = DEFAULT_TIMEOUT_MS } = options;
|
|
149
|
+
requireEndpoint(endpoint);
|
|
150
|
+
requireTimeout(timeoutMs);
|
|
151
|
+
requireValidFeedback(feedback);
|
|
152
|
+
const body = buildBody(feedback);
|
|
153
|
+
const controller = new AbortController();
|
|
154
|
+
const timeout = startTimeout(controller, timeoutMs);
|
|
155
|
+
try {
|
|
156
|
+
const response = await Promise.race([post(fetchFunction, endpoint, bearer, body, controller.signal), timeout.expired]);
|
|
157
|
+
discardBody(response);
|
|
158
|
+
assertOk(response);
|
|
159
|
+
} catch (error) {
|
|
160
|
+
throw toFeedbackError(error, controller, timeoutMs);
|
|
161
|
+
} finally {
|
|
162
|
+
timeout.cancel();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
export { DEFAULT_FEEDBACK_ENDPOINT, DEFAULT_TIMEOUT_MS, FEEDBACK_LIMITS, FeedbackError, sendFeedback, validateFeedback };
|
package/package.json
CHANGED
|
@@ -1 +1,32 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"name": "@neon-pixels/feedback-client",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Send user feedback to the Neon Pixels dashboard: one typed request, a typed error, and the receiver's field limits.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/neonpixels-studio/packages.git",
|
|
10
|
+
"directory": "packages/feedback-client"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": "^22.17.0 || ^24.0.0 || >=26.0.0"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsdown",
|
|
30
|
+
"typecheck": "tsc --noEmit -p tsconfig.json"
|
|
31
|
+
}
|
|
32
|
+
}
|