fix: resolve all lint warnings and fix failing playground toolbar test
- Fix playground-toolbar test: URL changed to github.com but test still expected docs.emdashcms.com - create-emdash: extract selectTemplate() to eliminate unsafe/unnecessary type assertions - create-emdash: use type-safe Object.keys filter instead of bare cast - cloudflare/cache: use Reflect.get with typeof guard instead of double type assertion - x402/enforcer: replace unsafe request cast with Reflect.get type guards for CF bot management - x402/middleware: suppress unavoidable virtual module any-cast with eslint comment
This commit is contained in:
4
packages/cloudflare/src/cache/runtime.ts
vendored
4
packages/cloudflare/src/cache/runtime.ts
vendored
@@ -134,7 +134,9 @@ function normalizeCacheKey(url: URL): string {
|
|||||||
*/
|
*/
|
||||||
function resolveEnvValue(explicit: string | undefined, envVarName: string): string | undefined {
|
function resolveEnvValue(explicit: string | undefined, envVarName: string): string | undefined {
|
||||||
if (explicit) return explicit;
|
if (explicit) return explicit;
|
||||||
return (env as Record<string, unknown>)[envVarName] as string | undefined;
|
if (!(envVarName in env)) return undefined;
|
||||||
|
const value: unknown = Reflect.get(env, envVarName);
|
||||||
|
return typeof value === "string" ? value : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ describe("renderPlaygroundToolbar", () => {
|
|||||||
it("renders the deploy CTA link", () => {
|
it("renders the deploy CTA link", () => {
|
||||||
const html = renderPlaygroundToolbar(BASE_CONFIG);
|
const html = renderPlaygroundToolbar(BASE_CONFIG);
|
||||||
expect(html).toContain("Deploy your own");
|
expect(html).toContain("Deploy your own");
|
||||||
expect(html).toContain("docs.emdashcms.com/getting-started");
|
expect(html).toContain("github.com/emdash-cms/emdash");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("renders reset and dismiss buttons", () => {
|
it("renders reset and dismiss buttons", () => {
|
||||||
|
|||||||
@@ -99,13 +99,39 @@ type CloudflareTemplate = keyof typeof CLOUDFLARE_TEMPLATES;
|
|||||||
function selectOptions<K extends string>(
|
function selectOptions<K extends string>(
|
||||||
obj: Readonly<Record<K, Readonly<{ name: string; description: string }>>>,
|
obj: Readonly<Record<K, Readonly<{ name: string; description: string }>>>,
|
||||||
): { value: K; label: string; hint: string }[] {
|
): { value: K; label: string; hint: string }[] {
|
||||||
return (Object.keys(obj) as K[]).map((key) => ({
|
const keys: K[] = Object.keys(obj).filter((k): k is K => k in obj);
|
||||||
|
return keys.map((key) => ({
|
||||||
value: key,
|
value: key,
|
||||||
label: obj[key].name,
|
label: obj[key].name,
|
||||||
hint: obj[key].description,
|
hint: obj[key].description,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function selectTemplate(platform: Platform): Promise<TemplateConfig> {
|
||||||
|
if (platform === "node") {
|
||||||
|
const key = await p.select<NodeTemplate>({
|
||||||
|
message: "Which template?",
|
||||||
|
options: selectOptions(NODE_TEMPLATES),
|
||||||
|
initialValue: "blog",
|
||||||
|
});
|
||||||
|
if (p.isCancel(key)) {
|
||||||
|
p.cancel("Operation cancelled.");
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
return NODE_TEMPLATES[key];
|
||||||
|
}
|
||||||
|
const key = await p.select<CloudflareTemplate>({
|
||||||
|
message: "Which template?",
|
||||||
|
options: selectOptions(CLOUDFLARE_TEMPLATES),
|
||||||
|
initialValue: "blog",
|
||||||
|
});
|
||||||
|
if (p.isCancel(key)) {
|
||||||
|
p.cancel("Operation cancelled.");
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
return CLOUDFLARE_TEMPLATES[key];
|
||||||
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
console.clear();
|
console.clear();
|
||||||
|
|
||||||
@@ -158,7 +184,7 @@ async function main() {
|
|||||||
hint: "SQLite + local file storage",
|
hint: "SQLite + local file storage",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
initialValue: "node",
|
initialValue: "cloudflare",
|
||||||
});
|
});
|
||||||
|
|
||||||
if (p.isCancel(platform)) {
|
if (p.isCancel(platform)) {
|
||||||
@@ -167,28 +193,7 @@ async function main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Step 2: pick template
|
// Step 2: pick template
|
||||||
const templateKey =
|
const templateConfig = await selectTemplate(platform);
|
||||||
platform === "node"
|
|
||||||
? await p.select<NodeTemplate>({
|
|
||||||
message: "Which template?",
|
|
||||||
options: selectOptions(NODE_TEMPLATES),
|
|
||||||
initialValue: "blog",
|
|
||||||
})
|
|
||||||
: await p.select<CloudflareTemplate>({
|
|
||||||
message: "Which template?",
|
|
||||||
options: selectOptions(CLOUDFLARE_TEMPLATES),
|
|
||||||
initialValue: "blog",
|
|
||||||
});
|
|
||||||
|
|
||||||
if (p.isCancel(templateKey)) {
|
|
||||||
p.cancel("Operation cancelled.");
|
|
||||||
process.exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
const templateConfig =
|
|
||||||
platform === "node"
|
|
||||||
? NODE_TEMPLATES[templateKey as NodeTemplate]
|
|
||||||
: CLOUDFLARE_TEMPLATES[templateKey as CloudflareTemplate];
|
|
||||||
|
|
||||||
// Step 3: pick package manager
|
// Step 3: pick package manager
|
||||||
const detectedPm = detectPackageManager();
|
const detectedPm = detectPackageManager();
|
||||||
|
|||||||
@@ -81,9 +81,12 @@ async function getResourceServer(config: X402Config): Promise<x402ResourceServer
|
|||||||
*/
|
*/
|
||||||
function isBot(request: Request, threshold: number): boolean {
|
function isBot(request: Request, threshold: number): boolean {
|
||||||
// Cloudflare Workers expose cf properties on the request
|
// Cloudflare Workers expose cf properties on the request
|
||||||
const cf = (request as unknown as { cf?: { botManagement?: { score?: number } } }).cf;
|
const cf: unknown = Reflect.get(request, "cf");
|
||||||
const score = cf?.botManagement?.score;
|
if (cf == null || typeof cf !== "object") return false;
|
||||||
if (score == null) return false;
|
const bm: unknown = Reflect.get(cf, "botManagement");
|
||||||
|
if (bm == null || typeof bm !== "object") return false;
|
||||||
|
const score: unknown = Reflect.get(bm, "score");
|
||||||
|
if (typeof score !== "number") return false;
|
||||||
return score < threshold;
|
return score < threshold;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ import x402Config from "virtual:x402/config";
|
|||||||
import { createEnforcer } from "./enforcer.js";
|
import { createEnforcer } from "./enforcer.js";
|
||||||
import type { X402Config } from "./types.js";
|
import type { X402Config } from "./types.js";
|
||||||
|
|
||||||
const config = x402Config as X402Config;
|
// eslint-disable-next-line typescript-eslint/no-unsafe-type-assertion -- virtual module import has no type info
|
||||||
|
const config: X402Config = x402Config as X402Config;
|
||||||
const enforcer = createEnforcer(config);
|
const enforcer = createEnforcer(config);
|
||||||
|
|
||||||
export const onRequest = defineMiddleware(async (context, next) => {
|
export const onRequest = defineMiddleware(async (context, next) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user