diff --git a/Source/Index.ts b/Source/Object/Worker.ts similarity index 96% rename from Source/Index.ts rename to Source/Object/Worker.ts index 80b7052..ff215fd 100644 --- a/Source/Index.ts +++ b/Source/Object/Worker.ts @@ -1,9 +1,3 @@ -import type { Request, ResponseInit } from "@cloudflare/workers-types"; - -export const { default: Environment } = await import("./Object/Environment.js"); - -export const { Response } = await import("@cloudflare/workers-types"); - class JSONResponse extends Response { constructor( // rome-ignore lint/suspicious/noExplicitAny: @@ -89,3 +83,9 @@ export default { return (await import("itty-router")).Router().handle(request); }, }; + +import type { Request, ResponseInit } from "@cloudflare/workers-types"; + +export const { default: Environment } = await import("./Environment.js"); + +export const { Response } = await import("@cloudflare/workers-types"); diff --git a/Target/Index.js b/Target/Index.js deleted file mode 100644 index b108798..0000000 --- a/Target/Index.js +++ /dev/null @@ -1 +0,0 @@ -const{default:r}=await import("./Object/Environment.js"),{Response:n}=await import("@cloudflare/workers-types");class a extends n{constructor(t,s={headers:{"content-type":"application/json;charset=UTF-8"}}){super(JSON.stringify(t),s)}}(await import("itty-router")).Router().post("/discord",async e=>{const t=await e.json();if(t.type===(await import("discord-interactions")).InteractionType.PING)return new a({type:(await import("discord-interactions")).InteractionResponseType.PONG});if(t.type===(await import("discord-interactions")).InteractionType.APPLICATION_COMMAND)switch(t.data.name.toLowerCase()){case"invite":return new a({type:4,data:{content:`https://discord.com/oauth2/authorize?client_id=${r.parse(process.env).DISCORD_APPLICATION_ID}&scope=applications.commands`,flags:64}});default:return new a({error:"Unknown Type"},{status:400})}return new a({error:"Unknown Type"},{status:400})}),(await import("itty-router")).Router().all("*",()=>new n("404 | Not Found.",{status:404}));var o={async fetch(e,{DISCORD_PUBLIC_KEY:t}=r.parse(process.env)){return e.method==="POST"&&!(await import("discord-interactions")).verifyKey(await e.clone().arrayBuffer(),e.headers.get("x-signature-ed25519")??"",e.headers.get("x-signature-timestamp")??"",t)?(console.error("Invalid Request"),new n("Bad request signature.",{status:401})):(await import("itty-router")).Router().handle(e)}};export{r as Environment,n as Response,o as default}; diff --git a/Target/Index.d.ts b/Target/Object/Worker.d.ts similarity index 100% rename from Target/Index.d.ts rename to Target/Object/Worker.d.ts index f695dc6..d927f4f 100644 --- a/Target/Index.d.ts +++ b/Target/Object/Worker.d.ts @@ -1,3 +1,13 @@ +declare const _default: { + fetch(request: Request, { DISCORD_PUBLIC_KEY }?: { + DISCORD_APPLICATION_ID: string; + DISCORD_CLIENT_ID: string; + DISCORD_CLIENT_SECRET: string; + DISCORD_PUBLIC_KEY: string; + DISCORD_TOKENS: string; + }): Promise; +}; +export default _default; import type { Request } from "@cloudflare/workers-types"; export declare const Environment: import("zod").ZodObject<{ DISCORD_APPLICATION_ID: import("zod").ZodDefault>; @@ -19,13 +29,3 @@ export declare const Environment: import("zod").ZodObject<{ DISCORD_TOKENS?: string | undefined; }>; export declare const Response: typeof import("@cloudflare/workers-types").Response; -declare const _default: { - fetch(request: Request, { DISCORD_PUBLIC_KEY }?: { - DISCORD_APPLICATION_ID: string; - DISCORD_CLIENT_ID: string; - DISCORD_CLIENT_SECRET: string; - DISCORD_PUBLIC_KEY: string; - DISCORD_TOKENS: string; - }): Promise; -}; -export default _default; diff --git a/Target/Object/Worker.js b/Target/Object/Worker.js new file mode 100644 index 0000000..a16a275 --- /dev/null +++ b/Target/Object/Worker.js @@ -0,0 +1 @@ +class a extends n{constructor(t,s={headers:{"content-type":"application/json;charset=UTF-8"}}){super(JSON.stringify(t),s)}}(await import("itty-router")).Router().post("/discord",async e=>{const t=await e.json();if(t.type===(await import("discord-interactions")).InteractionType.PING)return new a({type:(await import("discord-interactions")).InteractionResponseType.PONG});if(t.type===(await import("discord-interactions")).InteractionType.APPLICATION_COMMAND)switch(t.data.name.toLowerCase()){case"invite":return new a({type:4,data:{content:`https://discord.com/oauth2/authorize?client_id=${r.parse(process.env).DISCORD_APPLICATION_ID}&scope=applications.commands`,flags:64}});default:return new a({error:"Unknown Type"},{status:400})}return new a({error:"Unknown Type"},{status:400})}),(await import("itty-router")).Router().all("*",()=>new n("404 | Not Found.",{status:404}));var o={async fetch(e,{DISCORD_PUBLIC_KEY:t}=r.parse(process.env)){return e.method==="POST"&&!(await import("discord-interactions")).verifyKey(await e.clone().arrayBuffer(),e.headers.get("x-signature-ed25519")??"",e.headers.get("x-signature-timestamp")??"",t)?(console.error("Invalid Request"),new n("Bad request signature.",{status:401})):(await import("itty-router")).Router().handle(e)}};const{default:r}=await import("./Environment.js"),{Response:n}=await import("@cloudflare/workers-types");export{r as Environment,n as Response,o as default}; diff --git a/package.json b/package.json index 56a61f0..2fcf483 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,8 @@ "url": "https://nikolahristov.tech" }, "type": "module", - "main": "./Target/Index.js", - "types": "./Target/Index.d.ts", + "main": "./Target/Object/Worker.js", + "types": "./Target/Object/Worker.d.ts", "scripts": { "dev": "wrangler dev", "prepublishOnly": "TypeScriptESBuild 'Source/**/*.ts'" diff --git a/wrangler.toml b/wrangler.toml index 6714400..6bb5fc8 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -1,4 +1,4 @@ name = "api" -main = "Source/Index.ts" +main = "./Target/Object/Worker.js" compatibility_date = "2023-02-24" send_metrics = false