@nightshift-sdk/fiber-vite 0.1.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/dist/index.d.ts +22 -0
- package/dist/index.js +135 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nightshift
|
|
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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
export interface FiberOptions {
|
|
3
|
+
/** Source root to scan (default "src"). */
|
|
4
|
+
root?: string;
|
|
5
|
+
/** Output file (default "src/fiber.generated.ts"). */
|
|
6
|
+
out?: string;
|
|
7
|
+
/** Override config; otherwise read from VITE_NIGHTSHIFT_* / NIGHTSHIFT_* env. */
|
|
8
|
+
baseUrl?: string;
|
|
9
|
+
org?: string;
|
|
10
|
+
token?: string;
|
|
11
|
+
/** Dev token-injecting proxy. `true` (default) mounts it at /api/nightshift;
|
|
12
|
+
* pass `{ path }` to change the mount, or `false` to disable. */
|
|
13
|
+
proxy?: boolean | {
|
|
14
|
+
path?: string;
|
|
15
|
+
};
|
|
16
|
+
/** Publish the query manifest on `vite build`. Pass `{ appId }` or set
|
|
17
|
+
* NIGHTSHIFT_APP_ID; uses NIGHTSHIFT_PUBLISH_TOKEN (falls back to the token). */
|
|
18
|
+
publish?: boolean | {
|
|
19
|
+
appId?: string;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export declare function fiber(options?: FiberOptions): Plugin;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { writeFile } from "node:fs/promises";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { generate, publish } from "@nightshift-sdk/fiber-cli";
|
|
4
|
+
import { createFiberProxy } from "@nightshift-sdk/fiber-proxy";
|
|
5
|
+
import { loadEnv } from "vite";
|
|
6
|
+
function fiber(options = {}) {
|
|
7
|
+
let cfg;
|
|
8
|
+
let rootDir;
|
|
9
|
+
let outDir;
|
|
10
|
+
let outFile;
|
|
11
|
+
let appId = "";
|
|
12
|
+
let publishToken = "";
|
|
13
|
+
let running = false;
|
|
14
|
+
let queued = false;
|
|
15
|
+
let timer;
|
|
16
|
+
return {
|
|
17
|
+
name: "fiber",
|
|
18
|
+
// Hosted apps serve under apps.nightshift.sh/<id>/, so emit relative asset
|
|
19
|
+
// URLs (./assets/…) — these work under a sub-path AND at root. Without this,
|
|
20
|
+
// Vite's default base "/" produces /assets/… which 404s when hosted. An
|
|
21
|
+
// explicit base in the user's config is respected.
|
|
22
|
+
config(userConfig) {
|
|
23
|
+
if (!userConfig.base || userConfig.base === "/") return { base: "./" };
|
|
24
|
+
},
|
|
25
|
+
configResolved(config) {
|
|
26
|
+
const env = loadEnv(config.mode, config.root, "");
|
|
27
|
+
cfg = {
|
|
28
|
+
baseUrl: options.baseUrl || env.VITE_NIGHTSHIFT_BASE_URL || env.NIGHTSHIFT_BASE_URL || "https://api.nightshift.sh",
|
|
29
|
+
org: options.org || env.VITE_NIGHTSHIFT_ORG || env.NIGHTSHIFT_ORG || "",
|
|
30
|
+
token: options.token || env.VITE_NIGHTSHIFT_TOKEN || env.NIGHTSHIFT_TOKEN || ""
|
|
31
|
+
};
|
|
32
|
+
rootDir = resolve(config.root, options.root ?? "src");
|
|
33
|
+
outDir = resolve(config.root, config.build.outDir);
|
|
34
|
+
outFile = resolve(config.root, options.out ?? "src/fiber.generated.ts");
|
|
35
|
+
appId = typeof options.publish === "object" && options.publish.appId || env.NIGHTSHIFT_APP_ID || "";
|
|
36
|
+
publishToken = env.NIGHTSHIFT_PUBLISH_TOKEN || cfg.token;
|
|
37
|
+
},
|
|
38
|
+
// Build: publish the manifest (opt-in). Only runs in `vite build`.
|
|
39
|
+
async closeBundle() {
|
|
40
|
+
if (!options.publish) return;
|
|
41
|
+
if (!appId) {
|
|
42
|
+
console.warn("[fiber] publish: set publish.appId or NIGHTSHIFT_APP_ID");
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (!cfg.org || !publishToken) {
|
|
46
|
+
console.warn("[fiber] publish: set NIGHTSHIFT_ORG and a publish token");
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
const { version, count, url } = await publish({ ...cfg, token: publishToken }, rootDir, appId, { dist: outDir });
|
|
51
|
+
console.info(`[fiber] publish: ${count} quer${count === 1 ? "y" : "ies"} → ${appId} version ${version}${url ? ` (${url})` : ""}`);
|
|
52
|
+
} catch (e) {
|
|
53
|
+
console.error(`[fiber] publish failed: ${e.message}`);
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
configureServer(server) {
|
|
57
|
+
const log = (msg, warn = false) => server.config.logger[warn ? "warn" : "info"](`[fiber] ${msg}`);
|
|
58
|
+
if (options.proxy !== false) {
|
|
59
|
+
const mount = typeof options.proxy === "object" && options.proxy.path || "/api/nightshift";
|
|
60
|
+
const handle = createFiberProxy({
|
|
61
|
+
token: cfg.token,
|
|
62
|
+
org: cfg.org,
|
|
63
|
+
baseUrl: cfg.baseUrl,
|
|
64
|
+
mountPath: mount
|
|
65
|
+
});
|
|
66
|
+
server.middlewares.use((req, res, next) => {
|
|
67
|
+
if (!req.url || !req.url.startsWith(mount)) return next();
|
|
68
|
+
forward(handle, req, res).catch(next);
|
|
69
|
+
});
|
|
70
|
+
log(`proxy on ${mount} -> ${cfg.baseUrl}${cfg.org ? "" : " (set NIGHTSHIFT_ORG/TOKEN)"}`);
|
|
71
|
+
}
|
|
72
|
+
const run = async () => {
|
|
73
|
+
if (running) {
|
|
74
|
+
queued = true;
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
running = true;
|
|
78
|
+
try {
|
|
79
|
+
const { content, count } = await generate(cfg, rootDir);
|
|
80
|
+
await writeFile(outFile, content);
|
|
81
|
+
log(`typegen: ${count} quer${count === 1 ? "y" : "ies"}`);
|
|
82
|
+
} catch (e) {
|
|
83
|
+
log(`typegen failed: ${e.message}`, true);
|
|
84
|
+
} finally {
|
|
85
|
+
running = false;
|
|
86
|
+
if (queued) {
|
|
87
|
+
queued = false;
|
|
88
|
+
void run();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const schedule = () => {
|
|
93
|
+
clearTimeout(timer);
|
|
94
|
+
timer = setTimeout(() => void run(), 300);
|
|
95
|
+
};
|
|
96
|
+
if (!cfg.org || !cfg.token) {
|
|
97
|
+
log("set NIGHTSHIFT_ORG and NIGHTSHIFT_TOKEN (server-side) to enable typegen + proxy", true);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
void run();
|
|
101
|
+
const onChange = (file) => {
|
|
102
|
+
if (resolve(file) === outFile) return;
|
|
103
|
+
if (/\.tsx?$/.test(file) && !file.endsWith(".generated.ts")) schedule();
|
|
104
|
+
};
|
|
105
|
+
server.watcher.on("change", onChange);
|
|
106
|
+
server.watcher.on("add", onChange);
|
|
107
|
+
server.watcher.on("unlink", onChange);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
async function forward(handle, req, res) {
|
|
112
|
+
const host = req.headers.host ?? "localhost";
|
|
113
|
+
const method = req.method ?? "GET";
|
|
114
|
+
const headers = new Headers();
|
|
115
|
+
for (const [k, v] of Object.entries(req.headers)) {
|
|
116
|
+
if (typeof v === "string") headers.set(k, v);
|
|
117
|
+
else if (Array.isArray(v)) headers.set(k, v.join(", "));
|
|
118
|
+
}
|
|
119
|
+
const body = method === "GET" || method === "HEAD" ? void 0 : await readBody(req);
|
|
120
|
+
const response = await handle(new Request(`http://${host}${req.url}`, { method, headers, body }));
|
|
121
|
+
res.statusCode = response.status;
|
|
122
|
+
response.headers.forEach((value, key) => res.setHeader(key, value));
|
|
123
|
+
res.end(Buffer.from(await response.arrayBuffer()));
|
|
124
|
+
}
|
|
125
|
+
function readBody(req) {
|
|
126
|
+
return new Promise((resolve2, reject) => {
|
|
127
|
+
const chunks = [];
|
|
128
|
+
req.on("data", (c) => chunks.push(c));
|
|
129
|
+
req.on("end", () => resolve2(Buffer.concat(chunks).toString("utf8")));
|
|
130
|
+
req.on("error", reject);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
export {
|
|
134
|
+
fiber
|
|
135
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nightshift-sdk/fiber-vite",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Vite plugin — regenerate Nightshift query types on dev start and on save",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Nightshift",
|
|
7
|
+
"homepage": "https://github.com/nightshiftco/platform/tree/main/sdk/packages/fiber-vite#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/nightshiftco/platform.git",
|
|
11
|
+
"directory": "sdk/packages/fiber-vite"
|
|
12
|
+
},
|
|
13
|
+
"bugs": "https://github.com/nightshiftco/platform/issues",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"default": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "vite build",
|
|
31
|
+
"dev": "vite build --watch",
|
|
32
|
+
"typecheck": "tsc --noEmit",
|
|
33
|
+
"prepublishOnly": "npm run build"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"vite": "^5"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@nightshift-sdk/fiber-cli": "^0.1.0",
|
|
40
|
+
"@nightshift-sdk/fiber-proxy": "^0.1.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^20.14.0",
|
|
44
|
+
"typescript": "^5.5.0",
|
|
45
|
+
"vite": "^5.4.0",
|
|
46
|
+
"vite-plugin-dts": "^4.2.0"
|
|
47
|
+
}
|
|
48
|
+
}
|