timeback 0.1.0 → 0.1.1
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 +289 -122
- package/dist/client/adapters/vue/SignInButton.vue +260 -0
- package/dist/client/adapters/vue/SignInButton.vue.d.ts +53 -0
- package/dist/client/adapters/vue/index.d.ts +43 -0
- package/dist/client/adapters/vue/index.d.ts.map +1 -0
- package/dist/client/adapters/vue/index.ts +48 -0
- package/dist/client/adapters/vue/provider.d.ts +94 -0
- package/dist/client/adapters/vue/provider.d.ts.map +1 -0
- package/dist/client/adapters/vue/provider.ts +147 -0
- package/dist/index.js +257 -21
- package/dist/server/adapters/nuxt.d.ts +96 -0
- package/dist/server/adapters/nuxt.d.ts.map +1 -0
- package/dist/server/adapters/nuxt.js +663 -0
- package/dist/server/adapters/tanstack-start.d.ts +40 -0
- package/dist/server/adapters/tanstack-start.d.ts.map +1 -0
- package/dist/server/adapters/types.d.ts +68 -0
- package/dist/server/adapters/types.d.ts.map +1 -1
- package/package.json +14 -2
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
8
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
9
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
10
|
+
for (let key of __getOwnPropNames(mod))
|
|
11
|
+
if (!__hasOwnProp.call(to, key))
|
|
12
|
+
__defProp(to, key, {
|
|
13
|
+
get: () => mod[key],
|
|
14
|
+
enumerable: true
|
|
15
|
+
});
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
19
|
+
|
|
20
|
+
// src/shared/constants.ts
|
|
21
|
+
var ROUTES = {
|
|
22
|
+
ACTIVITY: "/activity",
|
|
23
|
+
IDENTITY: {
|
|
24
|
+
SIGNIN: "/identity/signin",
|
|
25
|
+
SIGNOUT: "/identity/signout",
|
|
26
|
+
CALLBACK: "/identity/callback"
|
|
27
|
+
},
|
|
28
|
+
USER: {
|
|
29
|
+
ME: "/user/me"
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// ../internal/logger/src/debug.ts
|
|
34
|
+
var patterns = null;
|
|
35
|
+
var debugAll = false;
|
|
36
|
+
var debugEnvSet = false;
|
|
37
|
+
function patternToRegex(pattern) {
|
|
38
|
+
const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
|
|
39
|
+
const regexStr = escaped.replace(/\*/g, ".*");
|
|
40
|
+
return new RegExp(`^${regexStr}$`);
|
|
41
|
+
}
|
|
42
|
+
function parseDebugEnv() {
|
|
43
|
+
if (patterns !== null)
|
|
44
|
+
return;
|
|
45
|
+
patterns = [];
|
|
46
|
+
if (typeof process === "undefined" || !process.env?.DEBUG) {
|
|
47
|
+
debugEnvSet = false;
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
debugEnvSet = true;
|
|
51
|
+
const debugValue = process.env.DEBUG.trim();
|
|
52
|
+
if (debugValue === "1" || debugValue === "true" || debugValue === "*") {
|
|
53
|
+
debugAll = true;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const parts = debugValue.split(",").map((p) => p.trim()).filter(Boolean);
|
|
57
|
+
for (const part of parts) {
|
|
58
|
+
if (part.startsWith("-")) {
|
|
59
|
+
patterns.push({
|
|
60
|
+
regex: patternToRegex(part.slice(1)),
|
|
61
|
+
exclude: true
|
|
62
|
+
});
|
|
63
|
+
} else {
|
|
64
|
+
patterns.push({
|
|
65
|
+
regex: patternToRegex(part),
|
|
66
|
+
exclude: false
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const hasInclude = patterns.some((p) => !p.exclude);
|
|
71
|
+
if (!hasInclude && patterns.length > 0) {
|
|
72
|
+
debugAll = true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function shouldShowDebug(scope) {
|
|
76
|
+
parseDebugEnv();
|
|
77
|
+
if (!debugEnvSet) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
if (debugAll) {
|
|
81
|
+
if (scope) {
|
|
82
|
+
for (const pattern of patterns) {
|
|
83
|
+
if (pattern.exclude && pattern.regex.test(scope)) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
if (!scope) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
for (const pattern of patterns) {
|
|
94
|
+
if (pattern.exclude && pattern.regex.test(scope)) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
for (const pattern of patterns) {
|
|
99
|
+
if (!pattern.exclude && pattern.regex.test(scope)) {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ../internal/logger/src/env.ts
|
|
107
|
+
function isBrowser() {
|
|
108
|
+
return typeof globalThis !== "undefined" && "window" in globalThis;
|
|
109
|
+
}
|
|
110
|
+
function detectEnvironment() {
|
|
111
|
+
if (isBrowser()) {
|
|
112
|
+
return "browser";
|
|
113
|
+
}
|
|
114
|
+
if (typeof process !== "undefined" && process.env) {
|
|
115
|
+
if (process.env["NODE_ENV"] === "test" || process.env["BUN_ENV"] === "test") {
|
|
116
|
+
return "test";
|
|
117
|
+
}
|
|
118
|
+
if (false) {}
|
|
119
|
+
if (process.env.CI || process.env.GITHUB_ACTIONS || process.env.GITLAB_CI || process.env.CIRCLECI || process.env.JENKINS_URL || process.env.BUILDKITE) {
|
|
120
|
+
return "ci";
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return "terminal";
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ../internal/logger/src/formatters/terminal.ts
|
|
127
|
+
var nodeInspect;
|
|
128
|
+
if (!isBrowser()) {
|
|
129
|
+
try {
|
|
130
|
+
const util = await import("node:util");
|
|
131
|
+
nodeInspect = util.inspect;
|
|
132
|
+
} catch {}
|
|
133
|
+
}
|
|
134
|
+
var colors = {
|
|
135
|
+
reset: "\x1B[0m",
|
|
136
|
+
bold: "\x1B[1m",
|
|
137
|
+
dim: "\x1B[2m",
|
|
138
|
+
red: "\x1B[31m",
|
|
139
|
+
yellow: "\x1B[33m",
|
|
140
|
+
blue: "\x1B[34m",
|
|
141
|
+
cyan: "\x1B[36m"
|
|
142
|
+
};
|
|
143
|
+
function getLevelColor(level) {
|
|
144
|
+
switch (level) {
|
|
145
|
+
case "debug":
|
|
146
|
+
return colors.blue;
|
|
147
|
+
case "info":
|
|
148
|
+
return colors.cyan;
|
|
149
|
+
case "warn":
|
|
150
|
+
return colors.yellow;
|
|
151
|
+
case "error":
|
|
152
|
+
return colors.red;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function getConsoleMethod(level) {
|
|
156
|
+
switch (level) {
|
|
157
|
+
case "debug":
|
|
158
|
+
return console.debug;
|
|
159
|
+
case "info":
|
|
160
|
+
return console.info;
|
|
161
|
+
case "warn":
|
|
162
|
+
return console.warn;
|
|
163
|
+
case "error":
|
|
164
|
+
return console.error;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function formatContext(context) {
|
|
168
|
+
if (nodeInspect) {
|
|
169
|
+
return nodeInspect(context, {
|
|
170
|
+
depth: null,
|
|
171
|
+
colors: true,
|
|
172
|
+
breakLength: 80,
|
|
173
|
+
compact: false
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
return JSON.stringify(context, null, 2);
|
|
177
|
+
}
|
|
178
|
+
var terminalFormatter = (entry) => {
|
|
179
|
+
const levelColor = getLevelColor(entry.level);
|
|
180
|
+
const consoleMethod = getConsoleMethod(entry.level);
|
|
181
|
+
const levelUpper = entry.level.toUpperCase().padEnd(5);
|
|
182
|
+
const isoString = entry.timestamp.toISOString().replace(/\.\d{3}Z$/, "");
|
|
183
|
+
const timestamp = `${colors.dim}[${isoString}]${colors.reset}`;
|
|
184
|
+
const level = `${levelColor}${levelUpper}${colors.reset}`;
|
|
185
|
+
const scope = entry.scope ? `${colors.bold}[${entry.scope}]${colors.reset} ` : "";
|
|
186
|
+
const prefix = `${timestamp} ${level} ${scope}${entry.message}`;
|
|
187
|
+
if (entry.context && Object.keys(entry.context).length > 0) {
|
|
188
|
+
consoleMethod(prefix, formatContext(entry.context));
|
|
189
|
+
} else {
|
|
190
|
+
consoleMethod(prefix);
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
// ../internal/logger/src/formatters/ci.ts
|
|
194
|
+
var LEVEL_PREFIX = {
|
|
195
|
+
debug: "[DEBUG]",
|
|
196
|
+
info: "[INFO]",
|
|
197
|
+
warn: "[WARN]",
|
|
198
|
+
error: "[ERROR]"
|
|
199
|
+
};
|
|
200
|
+
function formatContext2(context) {
|
|
201
|
+
return Object.entries(context).map(([key, value]) => `${key}=${formatValue(value)}`).join(" ");
|
|
202
|
+
}
|
|
203
|
+
function formatValue(value) {
|
|
204
|
+
if (typeof value === "string")
|
|
205
|
+
return value;
|
|
206
|
+
if (typeof value === "number")
|
|
207
|
+
return String(value);
|
|
208
|
+
if (typeof value === "boolean")
|
|
209
|
+
return String(value);
|
|
210
|
+
if (value === null)
|
|
211
|
+
return "null";
|
|
212
|
+
if (value === undefined)
|
|
213
|
+
return "undefined";
|
|
214
|
+
return JSON.stringify(value);
|
|
215
|
+
}
|
|
216
|
+
var ciFormatter = (entry) => {
|
|
217
|
+
const parts = [];
|
|
218
|
+
parts.push(entry.timestamp.toISOString());
|
|
219
|
+
parts.push(LEVEL_PREFIX[entry.level]);
|
|
220
|
+
if (entry.scope) {
|
|
221
|
+
parts.push(`[${entry.scope}]`);
|
|
222
|
+
}
|
|
223
|
+
parts.push(entry.message);
|
|
224
|
+
if (entry.context && Object.keys(entry.context).length > 0) {
|
|
225
|
+
parts.push(formatContext2(entry.context));
|
|
226
|
+
}
|
|
227
|
+
console.log(parts.join(" "));
|
|
228
|
+
};
|
|
229
|
+
// ../internal/logger/src/formatters/production.ts
|
|
230
|
+
var productionFormatter = (entry) => {
|
|
231
|
+
const output = {
|
|
232
|
+
timestamp: entry.timestamp.toISOString(),
|
|
233
|
+
level: entry.level,
|
|
234
|
+
...entry.scope && { scope: entry.scope },
|
|
235
|
+
msg: entry.message
|
|
236
|
+
};
|
|
237
|
+
if (entry.context && Object.keys(entry.context).length > 0) {
|
|
238
|
+
Object.assign(output, entry.context);
|
|
239
|
+
}
|
|
240
|
+
console.log(JSON.stringify(output));
|
|
241
|
+
};
|
|
242
|
+
// ../internal/logger/src/formatters/browser.ts
|
|
243
|
+
var LEVEL_STYLES = {
|
|
244
|
+
debug: "color: gray",
|
|
245
|
+
info: "color: #0ea5e9",
|
|
246
|
+
warn: "color: #f59e0b",
|
|
247
|
+
error: "color: #ef4444; font-weight: bold"
|
|
248
|
+
};
|
|
249
|
+
var LEVEL_METHODS = {
|
|
250
|
+
debug: "log",
|
|
251
|
+
info: "info",
|
|
252
|
+
warn: "warn",
|
|
253
|
+
error: "error"
|
|
254
|
+
};
|
|
255
|
+
var browserFormatter = (entry) => {
|
|
256
|
+
const method = LEVEL_METHODS[entry.level];
|
|
257
|
+
const style = LEVEL_STYLES[entry.level];
|
|
258
|
+
const prefix = entry.scope ? `[${entry.scope}]` : "";
|
|
259
|
+
const label = `%c${prefix} ${entry.message}`;
|
|
260
|
+
if (entry.context && Object.keys(entry.context).length > 0) {
|
|
261
|
+
console[method](label, style, entry.context);
|
|
262
|
+
} else {
|
|
263
|
+
console[method](label, style);
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
// ../internal/logger/src/logger.ts
|
|
267
|
+
var LOG_LEVELS = ["debug", "info", "warn", "error"];
|
|
268
|
+
function getFormatter(env) {
|
|
269
|
+
switch (env) {
|
|
270
|
+
case "terminal":
|
|
271
|
+
return terminalFormatter;
|
|
272
|
+
case "ci":
|
|
273
|
+
return ciFormatter;
|
|
274
|
+
case "production":
|
|
275
|
+
return productionFormatter;
|
|
276
|
+
case "browser":
|
|
277
|
+
return browserFormatter;
|
|
278
|
+
case "test":
|
|
279
|
+
return () => {};
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
function getDefaultMinLevel() {
|
|
283
|
+
if (typeof process !== "undefined" && process.env?.DEBUG) {
|
|
284
|
+
return "debug";
|
|
285
|
+
}
|
|
286
|
+
return "info";
|
|
287
|
+
}
|
|
288
|
+
function shouldLog(level, minLevel) {
|
|
289
|
+
return LOG_LEVELS.indexOf(level) >= LOG_LEVELS.indexOf(minLevel);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
class Logger {
|
|
293
|
+
scope;
|
|
294
|
+
minLevel;
|
|
295
|
+
environment;
|
|
296
|
+
formatter;
|
|
297
|
+
defaultContext;
|
|
298
|
+
constructor(options = {}) {
|
|
299
|
+
this.scope = options.scope;
|
|
300
|
+
this.minLevel = options.minLevel ?? getDefaultMinLevel();
|
|
301
|
+
this.defaultContext = options.defaultContext ?? {};
|
|
302
|
+
this.environment = options.environment ?? detectEnvironment();
|
|
303
|
+
this.formatter = getFormatter(this.environment);
|
|
304
|
+
}
|
|
305
|
+
child(scope) {
|
|
306
|
+
const childScope = this.scope ? `${this.scope}:${scope}` : scope;
|
|
307
|
+
return new Logger({
|
|
308
|
+
scope: childScope,
|
|
309
|
+
minLevel: this.minLevel,
|
|
310
|
+
environment: this.environment,
|
|
311
|
+
defaultContext: { ...this.defaultContext }
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
withContext(context) {
|
|
315
|
+
return new Logger({
|
|
316
|
+
scope: this.scope,
|
|
317
|
+
minLevel: this.minLevel,
|
|
318
|
+
environment: this.environment,
|
|
319
|
+
defaultContext: { ...this.defaultContext, ...context }
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
debug(message, context) {
|
|
323
|
+
this.log("debug", message, context);
|
|
324
|
+
}
|
|
325
|
+
info(message, context) {
|
|
326
|
+
this.log("info", message, context);
|
|
327
|
+
}
|
|
328
|
+
warn(message, context) {
|
|
329
|
+
this.log("warn", message, context);
|
|
330
|
+
}
|
|
331
|
+
error(message, context) {
|
|
332
|
+
this.log("error", message, context);
|
|
333
|
+
}
|
|
334
|
+
log(level, message, context) {
|
|
335
|
+
if (level === "debug" && !shouldShowDebug(this.scope)) {
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (!shouldLog(level, this.minLevel)) {
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const entry = {
|
|
342
|
+
level,
|
|
343
|
+
message,
|
|
344
|
+
scope: this.scope,
|
|
345
|
+
context: context || Object.keys(this.defaultContext).length > 0 ? { ...this.defaultContext, ...context } : undefined,
|
|
346
|
+
timestamp: new Date
|
|
347
|
+
};
|
|
348
|
+
this.formatter(entry);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
function createLogger(options = {}) {
|
|
352
|
+
return new Logger(options);
|
|
353
|
+
}
|
|
354
|
+
// src/server/lib/logger.ts
|
|
355
|
+
function isDebug() {
|
|
356
|
+
try {
|
|
357
|
+
const debug = typeof process === "undefined" ? undefined : process.env.DEBUG;
|
|
358
|
+
return debug === "1" || debug === "true";
|
|
359
|
+
} catch {
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
function createScopedLogger(scope) {
|
|
364
|
+
return createLogger({
|
|
365
|
+
scope: `timeback:${scope}`,
|
|
366
|
+
minLevel: isDebug() ? "debug" : "warn"
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
var ssoLog = createScopedLogger("sso");
|
|
370
|
+
var oidcLog = createScopedLogger("oidc");
|
|
371
|
+
|
|
372
|
+
// src/server/lib/oidc.ts
|
|
373
|
+
var discoveryCache = new Map;
|
|
374
|
+
async function fetchDiscoveryDocument(issuer) {
|
|
375
|
+
const cached = discoveryCache.get(issuer);
|
|
376
|
+
if (cached) {
|
|
377
|
+
return cached;
|
|
378
|
+
}
|
|
379
|
+
const url = `${issuer}/.well-known/openid-configuration`;
|
|
380
|
+
const response = await fetch(url);
|
|
381
|
+
if (!response.ok) {
|
|
382
|
+
oidcLog.error("Discovery fetch failed", { status: response.status });
|
|
383
|
+
throw new Error(`Failed to fetch OIDC discovery: ${response.statusText}`);
|
|
384
|
+
}
|
|
385
|
+
const doc = await response.json();
|
|
386
|
+
oidcLog.debug("Fetched OIDC discovery document", {
|
|
387
|
+
authEndpoint: doc.authorization_endpoint,
|
|
388
|
+
tokenEndpoint: doc.token_endpoint
|
|
389
|
+
});
|
|
390
|
+
discoveryCache.set(issuer, doc);
|
|
391
|
+
return doc;
|
|
392
|
+
}
|
|
393
|
+
function getIssuer(env) {
|
|
394
|
+
switch (env) {
|
|
395
|
+
case "production":
|
|
396
|
+
return "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_3uhuoRM3R";
|
|
397
|
+
case "staging":
|
|
398
|
+
return "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_5EUwTP9XD";
|
|
399
|
+
case "local":
|
|
400
|
+
throw new Error("Local environment is not yet supported for OIDC");
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
async function buildAuthorizationUrl(params) {
|
|
404
|
+
const discovery = await fetchDiscoveryDocument(params.issuer);
|
|
405
|
+
const url = new URL(discovery.authorization_endpoint);
|
|
406
|
+
url.searchParams.set("response_type", "code");
|
|
407
|
+
url.searchParams.set("client_id", params.clientId);
|
|
408
|
+
url.searchParams.set("redirect_uri", params.redirectUri);
|
|
409
|
+
url.searchParams.set("scope", "openid profile email");
|
|
410
|
+
url.searchParams.set("state", params.state);
|
|
411
|
+
return url.toString();
|
|
412
|
+
}
|
|
413
|
+
async function exchangeCodeForTokens(params) {
|
|
414
|
+
const discovery = await fetchDiscoveryDocument(params.issuer);
|
|
415
|
+
const response = await fetch(discovery.token_endpoint, {
|
|
416
|
+
method: "POST",
|
|
417
|
+
headers: {
|
|
418
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
419
|
+
},
|
|
420
|
+
body: new URLSearchParams({
|
|
421
|
+
grant_type: "authorization_code",
|
|
422
|
+
client_id: params.clientId,
|
|
423
|
+
client_secret: params.clientSecret,
|
|
424
|
+
code: params.code,
|
|
425
|
+
redirect_uri: params.redirectUri
|
|
426
|
+
})
|
|
427
|
+
});
|
|
428
|
+
if (!response.ok) {
|
|
429
|
+
const text = await response.text();
|
|
430
|
+
oidcLog.error("Token exchange failed", { status: response.status, body: text });
|
|
431
|
+
throw new Error(`Token exchange failed: ${response.status} ${text}`);
|
|
432
|
+
}
|
|
433
|
+
const tokens = await response.json();
|
|
434
|
+
oidcLog.debug("Received tokens from IdP", { expiresIn: tokens.expires_in });
|
|
435
|
+
return tokens;
|
|
436
|
+
}
|
|
437
|
+
async function getUserInfo(params) {
|
|
438
|
+
const discovery = await fetchDiscoveryDocument(params.issuer);
|
|
439
|
+
const response = await fetch(discovery.userinfo_endpoint, {
|
|
440
|
+
headers: {
|
|
441
|
+
Authorization: `Bearer ${params.accessToken}`
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
if (!response.ok) {
|
|
445
|
+
throw new Error(`UserInfo request failed: ${response.statusText}`);
|
|
446
|
+
}
|
|
447
|
+
return response.json();
|
|
448
|
+
}
|
|
449
|
+
// src/server/lib/utils.ts
|
|
450
|
+
function jsonResponse(data, status = 200, headers) {
|
|
451
|
+
const responseHeaders = new Headers(headers);
|
|
452
|
+
responseHeaders.set("Content-Type", "application/json");
|
|
453
|
+
return new Response(JSON.stringify(data), { status, headers: responseHeaders });
|
|
454
|
+
}
|
|
455
|
+
function redirectResponse(url, headers) {
|
|
456
|
+
const responseHeaders = new Headers(headers);
|
|
457
|
+
responseHeaders.set("Location", url);
|
|
458
|
+
return new Response(null, { status: 302, headers: responseHeaders });
|
|
459
|
+
}
|
|
460
|
+
function encodeBase64Url(data) {
|
|
461
|
+
const json = JSON.stringify(data);
|
|
462
|
+
return btoa(json).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
463
|
+
}
|
|
464
|
+
function decodeBase64Url(encoded) {
|
|
465
|
+
const padded = encoded.replace(/-/g, "+").replace(/_/g, "/");
|
|
466
|
+
const json = atob(padded);
|
|
467
|
+
return JSON.parse(json);
|
|
468
|
+
}
|
|
469
|
+
// src/server/adapters/utils.ts
|
|
470
|
+
function getHandlers(input) {
|
|
471
|
+
return "handle" in input ? input.handle : input;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// src/server/adapters/nuxt.ts
|
|
475
|
+
function bufferNodeBody(req) {
|
|
476
|
+
return new Promise((resolve, reject) => {
|
|
477
|
+
const chunks = [];
|
|
478
|
+
req.on("data", (chunk) => {
|
|
479
|
+
if (!chunk)
|
|
480
|
+
return;
|
|
481
|
+
if (chunk instanceof Buffer)
|
|
482
|
+
return void chunks.push(chunk);
|
|
483
|
+
if (typeof chunk === "string")
|
|
484
|
+
return void chunks.push(Buffer.from(chunk, "utf-8"));
|
|
485
|
+
if (chunk instanceof Uint8Array)
|
|
486
|
+
return void chunks.push(Buffer.from(chunk));
|
|
487
|
+
});
|
|
488
|
+
req.on("end", () => resolve(Buffer.concat(chunks).toString("utf-8")));
|
|
489
|
+
req.on("error", (chunk) => {
|
|
490
|
+
if (chunk instanceof Error) {
|
|
491
|
+
reject(chunk);
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
function getPathFromEvent(event) {
|
|
497
|
+
if (event.path) {
|
|
498
|
+
return event.path;
|
|
499
|
+
}
|
|
500
|
+
if (event.request) {
|
|
501
|
+
const url = event.request.url;
|
|
502
|
+
if (url.startsWith("http://") || url.startsWith("https://")) {
|
|
503
|
+
return new URL(url).pathname;
|
|
504
|
+
}
|
|
505
|
+
return url.split("?")[0] ?? "/";
|
|
506
|
+
}
|
|
507
|
+
const reqUrl = event.node?.req?.url;
|
|
508
|
+
return (reqUrl?.split("?")[0] ?? "/") || "/";
|
|
509
|
+
}
|
|
510
|
+
function firstHeaderValue(value) {
|
|
511
|
+
if (!value)
|
|
512
|
+
return;
|
|
513
|
+
if (Array.isArray(value))
|
|
514
|
+
return value[0]?.trim();
|
|
515
|
+
return value.split(",")[0]?.trim();
|
|
516
|
+
}
|
|
517
|
+
function buildAbsoluteUrlFromNodeRequest(req) {
|
|
518
|
+
const rawUrl = req.url ?? "/";
|
|
519
|
+
if (rawUrl.startsWith("http://") || rawUrl.startsWith("https://")) {
|
|
520
|
+
return new URL(rawUrl).href;
|
|
521
|
+
}
|
|
522
|
+
const forwardedProto = firstHeaderValue(req.headers["x-forwarded-proto"]);
|
|
523
|
+
const protocol = forwardedProto === "https" ? "https" : forwardedProto === "http" ? "http" : "http";
|
|
524
|
+
const forwardedHost = firstHeaderValue(req.headers["x-forwarded-host"]);
|
|
525
|
+
const host = forwardedHost || req.headers.host || "localhost";
|
|
526
|
+
return new URL(rawUrl, `${protocol}://${host}`).href;
|
|
527
|
+
}
|
|
528
|
+
function getUrlFromEvent(event) {
|
|
529
|
+
if (event.request) {
|
|
530
|
+
return event.request.url;
|
|
531
|
+
}
|
|
532
|
+
const req = event.node?.req;
|
|
533
|
+
if (!req) {
|
|
534
|
+
throw new Error("Cannot determine URL from Nuxt event");
|
|
535
|
+
}
|
|
536
|
+
return buildAbsoluteUrlFromNodeRequest(req);
|
|
537
|
+
}
|
|
538
|
+
function getMethodFromEvent(event) {
|
|
539
|
+
if (event.request) {
|
|
540
|
+
return event.request.method;
|
|
541
|
+
}
|
|
542
|
+
return event.node?.req?.method ?? "GET";
|
|
543
|
+
}
|
|
544
|
+
async function toWebRequest(event) {
|
|
545
|
+
if (event.request) {
|
|
546
|
+
return event.request;
|
|
547
|
+
}
|
|
548
|
+
const req = event.node?.req;
|
|
549
|
+
if (!req) {
|
|
550
|
+
throw new Error("Cannot convert Nuxt event to Request: no request object found");
|
|
551
|
+
}
|
|
552
|
+
const url = getUrlFromEvent(event);
|
|
553
|
+
const method = req.method ?? "GET";
|
|
554
|
+
const headers = new Headers;
|
|
555
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
556
|
+
if (value) {
|
|
557
|
+
headers.set(key, Array.isArray(value) ? value.join(", ") : value);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
const init = {
|
|
561
|
+
method,
|
|
562
|
+
headers
|
|
563
|
+
};
|
|
564
|
+
if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
|
|
565
|
+
const body = await bufferNodeBody(req);
|
|
566
|
+
if (body) {
|
|
567
|
+
init.body = body;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
return new Request(url, init);
|
|
571
|
+
}
|
|
572
|
+
function isTimebackPath(pathname, basePath) {
|
|
573
|
+
const normalizedBase = basePath.endsWith("/") ? basePath.slice(0, -1) : basePath;
|
|
574
|
+
return pathname === normalizedBase || pathname.startsWith(`${normalizedBase}/`);
|
|
575
|
+
}
|
|
576
|
+
async function nuxtHandler(options) {
|
|
577
|
+
const { timeback, event, basePath = "/api/timeback", callbackPath } = options;
|
|
578
|
+
const path = getPathFromEvent(event);
|
|
579
|
+
const method = getMethodFromEvent(event);
|
|
580
|
+
const handle = getHandlers(timeback);
|
|
581
|
+
if (callbackPath && path === callbackPath && method === "GET") {
|
|
582
|
+
const request = await toWebRequest(event);
|
|
583
|
+
return handle.identity.callback(request);
|
|
584
|
+
}
|
|
585
|
+
if (!isTimebackPath(path, basePath)) {
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
if (method === "GET") {
|
|
589
|
+
if (path.endsWith(ROUTES.IDENTITY.SIGNIN)) {
|
|
590
|
+
const request = await toWebRequest(event);
|
|
591
|
+
return handle.identity.signIn(request);
|
|
592
|
+
}
|
|
593
|
+
if (path.endsWith(ROUTES.IDENTITY.CALLBACK)) {
|
|
594
|
+
const request = await toWebRequest(event);
|
|
595
|
+
return handle.identity.callback(request);
|
|
596
|
+
}
|
|
597
|
+
if (path.endsWith(ROUTES.IDENTITY.SIGNOUT)) {
|
|
598
|
+
return handle.identity.signOut();
|
|
599
|
+
}
|
|
600
|
+
if (path.endsWith(ROUTES.USER.ME)) {
|
|
601
|
+
const request = await toWebRequest(event);
|
|
602
|
+
return handle.user.me(request);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
if (method === "POST") {
|
|
606
|
+
if (path.endsWith(ROUTES.ACTIVITY)) {
|
|
607
|
+
const request = await toWebRequest(event);
|
|
608
|
+
return handle.activity(request);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
function toNuxtHandler(input) {
|
|
614
|
+
const options = isHandlerOptions(input) ? input : { timeback: input };
|
|
615
|
+
const { timeback, callbackPath } = options;
|
|
616
|
+
const handle = getHandlers(timeback);
|
|
617
|
+
const routeHandler = async (event) => {
|
|
618
|
+
const path = getPathFromEvent(event);
|
|
619
|
+
const method = getMethodFromEvent(event);
|
|
620
|
+
if (callbackPath && path === callbackPath && method === "GET") {
|
|
621
|
+
const request = await toWebRequest(event);
|
|
622
|
+
return handle.identity.callback(request);
|
|
623
|
+
}
|
|
624
|
+
if (method === "GET") {
|
|
625
|
+
if (path.endsWith(ROUTES.IDENTITY.SIGNIN)) {
|
|
626
|
+
const request = await toWebRequest(event);
|
|
627
|
+
return handle.identity.signIn(request);
|
|
628
|
+
}
|
|
629
|
+
if (path.endsWith(ROUTES.IDENTITY.CALLBACK)) {
|
|
630
|
+
const request = await toWebRequest(event);
|
|
631
|
+
return handle.identity.callback(request);
|
|
632
|
+
}
|
|
633
|
+
if (path.endsWith(ROUTES.IDENTITY.SIGNOUT)) {
|
|
634
|
+
return handle.identity.signOut();
|
|
635
|
+
}
|
|
636
|
+
if (path.endsWith(ROUTES.USER.ME)) {
|
|
637
|
+
const request = await toWebRequest(event);
|
|
638
|
+
return handle.user.me(request);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
if (method === "POST") {
|
|
642
|
+
if (path.endsWith(ROUTES.ACTIVITY)) {
|
|
643
|
+
const request = await toWebRequest(event);
|
|
644
|
+
return handle.activity(request);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
return jsonResponse({ error: "Not found" }, 404);
|
|
648
|
+
};
|
|
649
|
+
return {
|
|
650
|
+
GET: routeHandler,
|
|
651
|
+
POST: routeHandler,
|
|
652
|
+
PUT: routeHandler,
|
|
653
|
+
DELETE: routeHandler,
|
|
654
|
+
PATCH: routeHandler
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
function isHandlerOptions(input) {
|
|
658
|
+
return typeof input === "object" && input !== null && "timeback" in input;
|
|
659
|
+
}
|
|
660
|
+
export {
|
|
661
|
+
toNuxtHandler,
|
|
662
|
+
nuxtHandler
|
|
663
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TanStack Start Adapter
|
|
3
|
+
*
|
|
4
|
+
* Adapts Timeback handlers for TanStack Start.
|
|
5
|
+
* Uses file-based API routes with TanStack's createFileRoute.
|
|
6
|
+
*/
|
|
7
|
+
import type { TanStackStartHandlerOptions, TanStackStartHandlers, TimebackInput } from './types';
|
|
8
|
+
/**
|
|
9
|
+
* Convert Timeback instance to TanStack Start route handlers.
|
|
10
|
+
*
|
|
11
|
+
* TanStack Start uses file-based routing with API routes exporting HTTP method handlers.
|
|
12
|
+
* This function creates handlers compatible with TanStack Start's route system.
|
|
13
|
+
*
|
|
14
|
+
* Accepts either the full Timeback instance or just the handlers:
|
|
15
|
+
* - `toTanStackStartHandler(timeback)`
|
|
16
|
+
* - `toTanStackStartHandler(timeback.handle)`
|
|
17
|
+
*
|
|
18
|
+
* @param input - Timeback instance or handlers, or options object
|
|
19
|
+
* @returns Object with HTTP method handlers for TanStack Start
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* // src/routes/api/timeback/$.ts
|
|
24
|
+
* import { createFileRoute } from '@tanstack/react-router'
|
|
25
|
+
* import { timeback } from '~/lib/timeback'
|
|
26
|
+
* import { toTanStackStartHandler } from 'timeback/tanstack-start'
|
|
27
|
+
*
|
|
28
|
+
* const handlers = toTanStackStartHandler(timeback)
|
|
29
|
+
*
|
|
30
|
+
* export const Route = createFileRoute('/api/timeback/$')({
|
|
31
|
+
* server: { handlers }
|
|
32
|
+
* })
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare function toTanStackStartHandler(input: TimebackInput | TanStackStartHandlerOptions): TanStackStartHandlers;
|
|
36
|
+
/**
|
|
37
|
+
* Alias for toTanStackStartHandler for consistency with other adapters.
|
|
38
|
+
*/
|
|
39
|
+
export declare const tanstackStartHandler: typeof toTanStackStartHandler;
|
|
40
|
+
//# sourceMappingURL=tanstack-start.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tanstack-start.d.ts","sourceRoot":"","sources":["../../../src/server/adapters/tanstack-start.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH,OAAO,KAAK,EAEX,2BAA2B,EAC3B,qBAAqB,EACrB,aAAa,EACb,MAAM,SAAS,CAAA;AAMhB;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,sBAAsB,CACrC,KAAK,EAAE,aAAa,GAAG,2BAA2B,GAChD,qBAAqB,CAgDvB;AAcD;;GAEG;AACH,eAAO,MAAM,oBAAoB,+BAAyB,CAAA"}
|