diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..60122e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,144 @@ +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# node.js +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) +web_modules/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next +out + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v2 +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* \ No newline at end of file diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..f3fc6be --- /dev/null +++ b/index.d.ts @@ -0,0 +1,58 @@ +export = Router +declare class Router { + protected routes: Route[] + protected corsConfig: RouterCorsConfig + connect(url: string, ...handlers: RouterHandler[]): Router + delete(url: string, ...handlers: RouterHandler[]): Router + get(url: string, ...handlers: RouterHandler[]): Router + head(url: string, ...handlers: RouterHandler[]): Router + options(url: string, ...handlers: RouterHandler[]): Router + patch(url: string, ...handlers: RouterHandler[]): Router + post(url: string, ...handlers: RouterHandler[]): Router + put(url: string, ...handlers: RouterHandler[]): Router + trace(url: string, ...handlers: RouterHandler[]): Router + any(url: string, ...handlers: RouterHandler[]): Router + all(url: string, ...handlers: RouterHandler[]): Router + cors(config: RouterCorsConfig): Router + private register(method: string, url: string, handlers: RouteHandler[]): Router + private getRoute(request: Request): Route | undefined + handle(event: Event): Response +} +declare namespace Router { + export { Route, RouterRequest, RouterResponse, RouterNext, RouterHandler, RouterCorsConfig } +} +type Route = { + method: string + url: string + handlers: RouterHandler[] +} +type RouterCorsConfig = { + allowOrigin: string + allowMethods: string + allowHeaders: string + maxAge: number + optionsSuccessStatus: number +} +type RouterHandler = (request: Request, response: Response, next: RouterNext) => any +type RouterRequest = { + method: string + params: { + [x: string]: string + } + headers: { + [x: string]: string + } + body: { + [x: string]: string + } | string +} +type RouterResponse = { + headers: { + [x: string]: string + } + status: number + body: { + [x: string]: string + } | string +} +type RouterNext = () => Promise \ No newline at end of file diff --git a/index.js b/index.js index bb90633..fa9a321 100644 --- a/index.js +++ b/index.js @@ -66,7 +66,7 @@ class Router { * @callback RouterHandler * @param {Request} request * @param {Response} response - * @param {next} next + * @param {RouterNext} next */ /** @@ -74,7 +74,7 @@ class Router { * * @param {string} url * @param {...RouterHandler} handlers - * @returns + * @returns {Router} */ connect(url, ...handlers) { return this.register('CONNECT', url, handlers) @@ -85,7 +85,7 @@ class Router { * * @param {string} url * @param {...RouterHandler} handlers - * @returns + * @returns {Router} */ delete(url, ...handlers) { return this.register('DELETE', url, handlers) @@ -96,7 +96,7 @@ class Router { * * @param {string} url * @param {...RouterHandler} handlers - * @returns + * @returns {Router} */ get(url, ...handlers) { return this.register('GET', url, handlers) @@ -107,7 +107,7 @@ class Router { * * @param {string} url * @param {...RouterHandler} handlers - * @returns + * @returns {Router} */ head(url, ...handlers) { return this.register('HEAD', url, handlers) @@ -118,7 +118,7 @@ class Router { * * @param {string} url * @param {...RouterHandler} handlers - * @returns + * @returns {Router} */ options(url, ...handlers) { return this.register('OPTIONS', url, handlers) @@ -129,7 +129,7 @@ class Router { * * @param {string} url * @param {...RouterHandler} handlers - * @returns + * @returns {Router} */ patch(url, ...handlers) { return this.register('PATCH', url, handlers) @@ -140,7 +140,7 @@ class Router { * * @param {string} url * @param {...RouterHandler} handlers - * @returns + * @returns {Router} */ post(url, ...handlers) { return this.register('POST', url, handlers) @@ -151,7 +151,7 @@ class Router { * * @param {string} url * @param {...RouterHandler} handlers - * @returns + * @returns {Router} */ put(url, ...handlers) { return this.register('PUT', url, handlers) @@ -162,7 +162,7 @@ class Router { * * @param {string} url * @param {...RouterHandler} handlers - * @returns + * @returns {Router} */ trace(url, ...handlers) { return this.register('TRACE', url, handlers) @@ -173,7 +173,7 @@ class Router { * * @param {string} url * @param {...RouterHandler} handlers - * @returns + * @returns {Router} */ any(url, ...handlers) { return this.register('*', url, handlers) diff --git a/package.json b/package.json index 9569333..494f740 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tsndr/cloudflare-worker-router", - "version": "1.0.2", + "version": "1.0.3", "description": "", "main": "index.js", "scripts": {},