@shiftapi/next 0.0.15
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 +20 -0
- package/dist/index.js +228 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Frank Chiarulli Jr.
|
|
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,20 @@
|
|
|
1
|
+
import { ShiftAPIPluginOptions } from 'shiftapi/internal';
|
|
2
|
+
export { ShiftAPIPluginOptions } from 'shiftapi/internal';
|
|
3
|
+
export { ShiftAPIConfig, defineConfig } from 'shiftapi';
|
|
4
|
+
|
|
5
|
+
type NextConfigObject = Record<string, unknown>;
|
|
6
|
+
type NextConfigFunction = (...args: unknown[]) => NextConfigObject | Promise<NextConfigObject>;
|
|
7
|
+
type NextConfig = NextConfigObject | NextConfigFunction;
|
|
8
|
+
/**
|
|
9
|
+
* Wraps a Next.js config with ShiftAPI integration (Go server management,
|
|
10
|
+
* typed API client generation, dev proxy).
|
|
11
|
+
*
|
|
12
|
+
* Supports both object and function configs.
|
|
13
|
+
*
|
|
14
|
+
* @param nextConfig - The user's exported Next.js config
|
|
15
|
+
* @param opts - ShiftAPI-specific options
|
|
16
|
+
* @returns The wrapped Next.js config (same shape as the input)
|
|
17
|
+
*/
|
|
18
|
+
declare function withShiftAPI<C extends NextConfig>(nextConfig?: C, opts?: ShiftAPIPluginOptions): C;
|
|
19
|
+
|
|
20
|
+
export { withShiftAPI };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { resolve } from "path";
|
|
3
|
+
import { watch } from "fs";
|
|
4
|
+
import { createRequire } from "module";
|
|
5
|
+
import {
|
|
6
|
+
loadConfig,
|
|
7
|
+
findConfigDir,
|
|
8
|
+
regenerateTypes as _regenerateTypes,
|
|
9
|
+
writeGeneratedFiles,
|
|
10
|
+
patchTsConfig,
|
|
11
|
+
nextClientJsTemplate,
|
|
12
|
+
DEV_API_PREFIX,
|
|
13
|
+
GoServerManager,
|
|
14
|
+
findFreePort
|
|
15
|
+
} from "shiftapi/internal";
|
|
16
|
+
import { defineConfig } from "shiftapi";
|
|
17
|
+
function isThenable(value) {
|
|
18
|
+
return typeof value === "object" && value !== null && typeof value.then === "function";
|
|
19
|
+
}
|
|
20
|
+
function withShiftAPI(nextConfig, opts) {
|
|
21
|
+
const cast = nextConfig ?? {};
|
|
22
|
+
if (typeof cast === "function") {
|
|
23
|
+
return function(...args) {
|
|
24
|
+
const result = cast.apply(this, args);
|
|
25
|
+
if (isThenable(result)) {
|
|
26
|
+
return result.then(
|
|
27
|
+
(cfg) => applyShiftAPI(cfg, opts)
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
return applyShiftAPI(result, opts);
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
return applyShiftAPI(cast, opts);
|
|
34
|
+
}
|
|
35
|
+
function applyShiftAPI(nextConfig, opts) {
|
|
36
|
+
const projectRoot = process.cwd();
|
|
37
|
+
const configDir = findConfigDir(projectRoot, opts?.configPath);
|
|
38
|
+
if (!configDir) {
|
|
39
|
+
console.warn(
|
|
40
|
+
"[shiftapi] Could not find shiftapi.config.ts. Skipping ShiftAPI integration."
|
|
41
|
+
);
|
|
42
|
+
return nextConfig;
|
|
43
|
+
}
|
|
44
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
45
|
+
const shiftapiClientPath = resolve(configDir, ".shiftapi", "client.js");
|
|
46
|
+
const require2 = createRequire(import.meta.url);
|
|
47
|
+
const openapiPath = require2.resolve("openapi-fetch");
|
|
48
|
+
const initPromise = initializeAsync(projectRoot, configDir, isDev, opts);
|
|
49
|
+
const patched = { ...nextConfig };
|
|
50
|
+
const existingWebpack = nextConfig.webpack;
|
|
51
|
+
patched.webpack = (config, context) => {
|
|
52
|
+
const cfg = existingWebpack ? existingWebpack(config, context) : config;
|
|
53
|
+
cfg.resolve = cfg.resolve || {};
|
|
54
|
+
cfg.resolve.alias = cfg.resolve.alias || {};
|
|
55
|
+
cfg.resolve.alias["@shiftapi/client"] = shiftapiClientPath;
|
|
56
|
+
cfg.resolve.alias["openapi-fetch"] = openapiPath;
|
|
57
|
+
cfg.plugins = cfg.plugins || [];
|
|
58
|
+
cfg.plugins.push({
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
60
|
+
apply(compiler) {
|
|
61
|
+
compiler.hooks.beforeCompile.tapPromise("ShiftAPI", async () => {
|
|
62
|
+
await initPromise;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
return cfg;
|
|
67
|
+
};
|
|
68
|
+
const existingExperimental = nextConfig.experimental ?? {};
|
|
69
|
+
const existingTurbo = existingExperimental.turbo ?? {};
|
|
70
|
+
const existingResolveAlias = existingTurbo.resolveAlias ?? {};
|
|
71
|
+
patched.experimental = {
|
|
72
|
+
...existingExperimental,
|
|
73
|
+
turbo: {
|
|
74
|
+
...existingTurbo,
|
|
75
|
+
resolveAlias: {
|
|
76
|
+
...existingResolveAlias,
|
|
77
|
+
"@shiftapi/client": shiftapiClientPath,
|
|
78
|
+
"openapi-fetch": openapiPath
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
if (isDev) {
|
|
83
|
+
const existingRewrites = nextConfig.rewrites;
|
|
84
|
+
patched.rewrites = async () => {
|
|
85
|
+
const { port } = await initPromise;
|
|
86
|
+
const shiftapiRewrite = {
|
|
87
|
+
source: `${DEV_API_PREFIX}/:path*`,
|
|
88
|
+
destination: `http://localhost:${port}/:path*`
|
|
89
|
+
};
|
|
90
|
+
if (!existingRewrites) {
|
|
91
|
+
return {
|
|
92
|
+
beforeFiles: [shiftapiRewrite],
|
|
93
|
+
afterFiles: [],
|
|
94
|
+
fallback: []
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
const existing = await existingRewrites();
|
|
98
|
+
if (Array.isArray(existing)) {
|
|
99
|
+
return {
|
|
100
|
+
beforeFiles: [shiftapiRewrite],
|
|
101
|
+
afterFiles: existing,
|
|
102
|
+
fallback: []
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
beforeFiles: [shiftapiRewrite, ...existing.beforeFiles ?? []],
|
|
107
|
+
afterFiles: existing.afterFiles ?? [],
|
|
108
|
+
fallback: existing.fallback ?? []
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
return patched;
|
|
113
|
+
}
|
|
114
|
+
async function initializeAsync(projectRoot, configDir, isDev, opts) {
|
|
115
|
+
const { config } = await loadConfig(projectRoot, opts?.configPath);
|
|
116
|
+
const serverEntry = config.server;
|
|
117
|
+
const baseUrl = config.baseUrl ?? "/";
|
|
118
|
+
const goRoot = configDir;
|
|
119
|
+
const parsedUrl = new URL(config.url ?? "http://localhost:8080");
|
|
120
|
+
const basePort = parseInt(parsedUrl.port || "8080");
|
|
121
|
+
if (isDev) {
|
|
122
|
+
return initializeDev(
|
|
123
|
+
projectRoot,
|
|
124
|
+
configDir,
|
|
125
|
+
serverEntry,
|
|
126
|
+
baseUrl,
|
|
127
|
+
goRoot,
|
|
128
|
+
parsedUrl,
|
|
129
|
+
basePort
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
return initializeBuild(projectRoot, configDir, serverEntry, baseUrl, goRoot, basePort);
|
|
133
|
+
}
|
|
134
|
+
async function initializeDev(projectRoot, configDir, serverEntry, baseUrl, goRoot, parsedUrl, basePort) {
|
|
135
|
+
const goPort = await findFreePort(basePort);
|
|
136
|
+
if (goPort !== basePort) {
|
|
137
|
+
console.log(`[shiftapi] Port ${basePort} is in use, using ${goPort}`);
|
|
138
|
+
}
|
|
139
|
+
const goServer = new GoServerManager(serverEntry, goRoot);
|
|
140
|
+
try {
|
|
141
|
+
await goServer.start(goPort);
|
|
142
|
+
console.log(
|
|
143
|
+
`[shiftapi] API docs available at ${parsedUrl.protocol}//${parsedUrl.hostname}:${goPort}/docs`
|
|
144
|
+
);
|
|
145
|
+
} catch (err) {
|
|
146
|
+
console.error("[shiftapi] Go server failed to start:", err);
|
|
147
|
+
}
|
|
148
|
+
let generatedDts = "";
|
|
149
|
+
try {
|
|
150
|
+
const result = await _regenerateTypes(serverEntry, goRoot, baseUrl, true, "");
|
|
151
|
+
generatedDts = result.types;
|
|
152
|
+
const clientJs = nextClientJsTemplate(goPort, baseUrl);
|
|
153
|
+
writeGeneratedFiles(configDir, generatedDts, baseUrl, { clientJsContent: clientJs });
|
|
154
|
+
patchTsConfig(projectRoot, configDir);
|
|
155
|
+
console.log("[shiftapi] Types generated.");
|
|
156
|
+
} catch (err) {
|
|
157
|
+
console.error("[shiftapi] Failed to generate types:", err);
|
|
158
|
+
}
|
|
159
|
+
let debounceTimer = null;
|
|
160
|
+
let watcher;
|
|
161
|
+
try {
|
|
162
|
+
watcher = watch(resolve(goRoot), { recursive: true }, (_event, filename) => {
|
|
163
|
+
if (!filename || !filename.endsWith(".go")) return;
|
|
164
|
+
console.log(`[shiftapi] Go file changed: ${filename}`);
|
|
165
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
166
|
+
debounceTimer = setTimeout(async () => {
|
|
167
|
+
try {
|
|
168
|
+
await goServer.stop();
|
|
169
|
+
await goServer.start(goPort);
|
|
170
|
+
const result = await _regenerateTypes(
|
|
171
|
+
serverEntry,
|
|
172
|
+
goRoot,
|
|
173
|
+
baseUrl,
|
|
174
|
+
true,
|
|
175
|
+
generatedDts
|
|
176
|
+
);
|
|
177
|
+
if (result.changed) {
|
|
178
|
+
generatedDts = result.types;
|
|
179
|
+
const clientJs = nextClientJsTemplate(goPort, baseUrl);
|
|
180
|
+
writeGeneratedFiles(configDir, generatedDts, baseUrl, {
|
|
181
|
+
clientJsContent: clientJs
|
|
182
|
+
});
|
|
183
|
+
console.log("[shiftapi] Types regenerated.");
|
|
184
|
+
}
|
|
185
|
+
} catch (err) {
|
|
186
|
+
console.error("[shiftapi] Failed to regenerate:", err);
|
|
187
|
+
}
|
|
188
|
+
}, 500);
|
|
189
|
+
});
|
|
190
|
+
} catch {
|
|
191
|
+
console.warn("[shiftapi] Could not set up file watcher for Go files.");
|
|
192
|
+
}
|
|
193
|
+
function cleanup() {
|
|
194
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
195
|
+
watcher?.close();
|
|
196
|
+
goServer.forceKill();
|
|
197
|
+
}
|
|
198
|
+
process.on("exit", cleanup);
|
|
199
|
+
process.on("SIGINT", async () => {
|
|
200
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
201
|
+
watcher?.close();
|
|
202
|
+
await goServer.stop();
|
|
203
|
+
process.exit();
|
|
204
|
+
});
|
|
205
|
+
process.on("SIGTERM", async () => {
|
|
206
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
207
|
+
watcher?.close();
|
|
208
|
+
await goServer.stop();
|
|
209
|
+
process.exit();
|
|
210
|
+
});
|
|
211
|
+
return { port: goPort };
|
|
212
|
+
}
|
|
213
|
+
async function initializeBuild(projectRoot, configDir, serverEntry, baseUrl, goRoot, basePort) {
|
|
214
|
+
try {
|
|
215
|
+
const result = await _regenerateTypes(serverEntry, goRoot, baseUrl, false, "");
|
|
216
|
+
const clientJs = nextClientJsTemplate(basePort, baseUrl);
|
|
217
|
+
writeGeneratedFiles(configDir, result.types, baseUrl, { clientJsContent: clientJs });
|
|
218
|
+
patchTsConfig(projectRoot, configDir);
|
|
219
|
+
console.log("[shiftapi] Types generated for build.");
|
|
220
|
+
} catch (err) {
|
|
221
|
+
console.error("[shiftapi] Failed to generate types for build:", err);
|
|
222
|
+
}
|
|
223
|
+
return { port: basePort };
|
|
224
|
+
}
|
|
225
|
+
export {
|
|
226
|
+
defineConfig,
|
|
227
|
+
withShiftAPI
|
|
228
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shiftapi/next",
|
|
3
|
+
"version": "0.0.15",
|
|
4
|
+
"description": "Next.js integration for fully-typed TypeScript clients from shiftapi Go servers",
|
|
5
|
+
"author": "Frank Chiarulli Jr. <frank@frankchiarulli.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/fcjr/shiftapi",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/fcjr/shiftapi",
|
|
11
|
+
"directory": "packages/next"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/fcjr/shiftapi/issues"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "dist/index.js",
|
|
18
|
+
"types": "dist/index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"next": ">=14.0.0"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"openapi-fetch": "^0.13.0",
|
|
33
|
+
"shiftapi": "0.0.15"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^25.2.3",
|
|
37
|
+
"next": "^15.0.0",
|
|
38
|
+
"tsup": "^8.0.0",
|
|
39
|
+
"typescript": "^5.5.0"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsup src/index.ts --format esm --dts",
|
|
43
|
+
"dev": "tsup src/index.ts --format esm --dts --watch"
|
|
44
|
+
}
|
|
45
|
+
}
|