@h3ravel/http 10.0.0 → 11.1.0
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 +13 -1
- package/dist/index.cjs +330 -371
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +196 -188
- package/dist/index.d.ts +196 -188
- package/dist/index.js +297 -345
- package/dist/index.js.map +1 -1
- package/dist/stubs/controller-resource.stub +34 -0
- package/dist/stubs/controller.stub +3 -0
- package/package.json +8 -8
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/Middleware.ts","../src/Request.ts","../src/Response.ts","../src/Contracts/HttpContract.ts","../src/Middleware/LogRequests.ts","../src/Providers/HttpServiceProvider.ts","../src/Resources/JsonResource.ts","../src/Resources/ApiResource.ts"],"sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from './Middleware';\nexport * from './Request';\nexport * from './Response';\nexport * from './Contracts/HttpContract';\nexport * from './Middleware/LogRequests';\nexport * from './Providers/HttpServiceProvider';\nexport * from './Resources/ApiResource';\nexport * from './Resources/JsonResource';\n","import { HttpContext } from './Contracts/HttpContract'\nimport { IMiddleware } from '@h3ravel/shared'\n\nexport abstract class Middleware implements IMiddleware {\n abstract handle (context: HttpContext, next: () => Promise<unknown>): Promise<unknown>\n}\n","import { getQuery, getRouterParams, readBody, type H3Event } from 'h3'\nimport { DotNestedKeys, DotNestedValue, safeDot } from '@h3ravel/support'\nimport type { ResponseHeaderMap, TypedHeaders } from 'fetchdts'\nimport { IRequest } from '@h3ravel/shared'\nimport { Application } from '@h3ravel/core'\n\nexport class Request implements IRequest {\n /**\n * Gets route parameters.\n * @returns An object containing route parameters.\n */\n readonly params: NonNullable<H3Event[\"context\"][\"params\"]>\n\n /**\n * Gets query parameters.\n * @returns An object containing query parameters.\n */\n readonly query: Record<string, string>;\n\n /**\n * Gets the request headers.\n * @returns An object containing request headers.\n */\n readonly headers: TypedHeaders<Record<keyof ResponseHeaderMap, string>>\n\n /**\n * The current H3 H3Event instance\n */\n private readonly event: H3Event\n\n constructor(\n event: H3Event,\n /**\n * The current app instance\n */\n public app: Application\n ) {\n this.event = event\n this.query = getQuery(this.event)\n this.params = getRouterParams(this.event)\n this.headers = this.event.req.headers\n }\n\n /**\n * Get all input data (query + body).\n */\n async all<T = Record<string, unknown>> (): Promise<T> {\n let data = {\n ...getRouterParams(this.event),\n ...getQuery(this.event),\n } as T\n\n if (this.event.req.method === 'POST') {\n data = Object.assign({}, data, Object.fromEntries((await this.event.req.formData()).entries()))\n } else if (this.event.req.method === 'PUT') {\n data = <never>Object.fromEntries(Object.entries(<never>await readBody(this.event)))\n }\n\n return data\n }\n\n /**\n * Get a single input field from query or body.\n */\n async input<T = unknown> (key: string, defaultValue?: T): Promise<T> {\n const data = await this.all<Record<string, T>>()\n return (data[key] ?? defaultValue) as T\n }\n\n /**\n * Get the base event\n */\n getEvent (): H3Event\n getEvent<K extends DotNestedKeys<H3Event>> (key: K): DotNestedValue<H3Event, K>\n getEvent<K extends DotNestedKeys<H3Event>> (key?: K): any {\n return safeDot(this.event, key)\n }\n}\n","import { DotNestedKeys, DotNestedValue, safeDot } from '@h3ravel/support'\nimport { html, redirect, } from 'h3'\n\nimport { Application } from '@h3ravel/core'\nimport type { H3Event } from 'h3'\nimport { IResponse } from '@h3ravel/shared'\n\nexport class Response implements IResponse {\n /**\n * The current H3 H3Event instance\n */\n private readonly event: H3Event\n\n private statusCode: number = 200\n private headers: Record<string, string> = {}\n\n constructor(\n event: H3Event,\n /**\n * The current app instance\n */\n public app: Application\n ) {\n this.event = event\n }\n\n /**\n * Set HTTP status code.\n */\n setStatusCode (code: number): this {\n this.statusCode = code\n this.event.res.status = code\n return this\n }\n\n /**\n * Set a header.\n */\n setHeader (name: string, value: string): this {\n this.headers[name] = value\n return this\n }\n\n html (content: string): string {\n this.applyHeaders()\n return html(this.event, content)\n }\n\n /**\n * Send a JSON response.\n */\n json<T = unknown> (data: T): T {\n this.setHeader('content-type', 'application/json; charset=utf-8')\n this.applyHeaders()\n return data\n }\n\n /**\n * Send plain text.\n */\n text (data: string): string {\n this.setHeader('content-type', 'text/plain; charset=utf-8')\n this.applyHeaders()\n return data\n }\n\n /**\n * Redirect to another URL.\n */\n redirect (url: string, status = 302): string {\n this.setStatusCode(status)\n return redirect(this.event, url, this.statusCode)\n }\n\n /**\n * Apply headers before sending response.\n */\n private applyHeaders (): void {\n Object.entries(this.headers).forEach(([key, value]) => {\n this.event.res.headers.set(key, value)\n })\n }\n\n /**\n * Get the base event\n */\n getEvent (): H3Event\n getEvent<K extends DotNestedKeys<H3Event>> (key: K): DotNestedValue<H3Event, K>\n getEvent<K extends DotNestedKeys<H3Event>> (key?: K): any {\n return safeDot(this.event, key)\n }\n}\n","export { HttpContext } from '@h3ravel/shared'\n","import { HttpContext } from '@h3ravel/shared'\nimport { Middleware } from '../Middleware'\n\nexport class LogRequests extends Middleware {\n async handle ({ request }: HttpContext, next: () => Promise<unknown>): Promise<unknown> {\n const url = request.getEvent('url')\n console.log(`[${request.getEvent('method')}] ${url.pathname + url.search}`)\n return next()\n }\n}\n","import { H3, serve } from 'h3'\n\nimport { ServiceProvider } from '@h3ravel/core'\n\n/**\n * Sets up HTTP kernel and request lifecycle.\n * \n * Register Request, Response, and Middleware classes.\n * Configure global middleware stack.\n * Boot HTTP kernel.\n * \n * Auto-Registered\n */\nexport class HttpServiceProvider extends ServiceProvider {\n public static priority = 998;\n\n register () {\n this.app.singleton('http.app', () => {\n return new H3()\n })\n\n this.app.singleton('http.serve', () => serve)\n }\n}\n","import { EventHandlerRequest, H3Event } from 'h3'\n\nexport interface Resource {\n [key: string]: any;\n pagination?: {\n from?: number | undefined;\n to?: number | undefined;\n perPage?: number | undefined;\n total?: number | undefined;\n } | undefined;\n}\n\ntype BodyResource = Resource & {\n data: Omit<Resource, 'pagination'>,\n meta?: {\n pagination?: Resource['pagination']\n } | undefined;\n}\n\n/**\n * Class to render API resource\n */\nexport class JsonResource<R extends Resource = any> {\n /**\n * The request instance\n */\n request: H3Event<EventHandlerRequest>['req']\n /**\n * The response instance\n */\n response: H3Event['res']\n /**\n * The data to send to the client\n */\n resource: R\n /**\n * The final response data object\n */\n body: BodyResource = {\n data: {},\n }\n /**\n * Flag to track if response should be sent automatically\n */\n private shouldSend: boolean = false\n /**\n * Flag to track if response has been sent\n */\n\n private responseSent: boolean = false;\n\n /**\n * Declare that this includes R's properties\n */\n [key: string]: any;\n\n /**\n * @param req The request instance\n * @param res The response instance\n * @param rsc The data to send to the client\n */\n constructor(protected event: H3Event, rsc: R) {\n this.request = event.req\n this.response = event.res\n this.resource = rsc\n\n // Copy all properties from rsc to this, avoiding conflicts\n for (const key of Object.keys(rsc)) {\n if (!(key in this)) {\n Object.defineProperty(this, key, {\n enumerable: true,\n configurable: true,\n get: () => this.resource[key],\n set: (value) => {\n (<any>this.resource)[key] = value\n },\n })\n }\n }\n }\n\n /**\n * Return the data in the expected format\n * \n * @returns \n */\n data (): Resource {\n return this.resource\n }\n\n /**\n * Build the response object\n * @returns this\n */\n json () {\n // Indicate response should be sent automatically\n this.shouldSend = true\n\n // Set default status code\n this.response.status = 200\n\n // Prepare body\n const resource = this.data()\n let data: Resource = Array.isArray(resource) ? [...resource] : { ...resource }\n\n if (typeof data.data !== 'undefined') {\n data = data.data\n }\n\n if (!Array.isArray(resource)) {\n delete data.pagination\n }\n\n this.body = {\n data,\n }\n\n // Set the pagination from the data() resource, if available\n if (!Array.isArray(resource) && resource.pagination) {\n const meta: BodyResource['meta'] = this.body.meta ?? {}\n meta.pagination = resource.pagination\n this.body.meta = meta\n }\n\n // If pagination is not available on the resource, then check and set it\n // if it's available on the base resource.\n if (this.resource.pagination && !this.body.meta?.pagination) {\n const meta: BodyResource['meta'] = this.body.meta ?? {}\n meta.pagination = this.resource.pagination\n this.body.meta = meta\n }\n\n return this\n }\n\n /**\n * Add context data to the response object\n * @param data Context data\n * @returns this\n */\n additional<X extends { [key: string]: any }> (data: X) {\n\n // Allow automatic send after additional\n this.shouldSend = true\n\n // Merge data with body\n delete data.data\n delete data.pagination\n\n this.body = {\n ...this.body,\n ...data,\n }\n\n return this\n }\n\n /**\n * Send the output to the client\n * @returns this\n */\n send () {\n this.shouldSend = false // Prevent automatic send\n if (!this.responseSent) {\n this.#send()\n }\n return this\n }\n\n /**\n * Set the status code for this response\n * @param code Status code\n * @returns this\n */\n status (code: number) {\n this.response.status = code\n return this\n }\n\n /**\n * Private method to send the response\n */\n #send () {\n if (!this.responseSent) {\n this.event.context.\n this.response.json(this.body)\n\n // Mark response as sent\n this.responseSent = true\n }\n }\n\n /**\n * Check if send should be triggered automatically\n */\n private checkSend () {\n if (this.shouldSend && !this.responseSent) {\n this.#send()\n }\n }\n}\n","import { JsonResource, Resource } from './JsonResource'\n\nimport { H3Event } from 'h3'\n\nexport function ApiResource (\n instance: JsonResource\n) {\n return new Proxy(instance, {\n get (target, prop, receiver) {\n const value = Reflect.get(target, prop, receiver)\n if (typeof value === 'function') {\n // Intercept json, additional, and send methods\n if (prop === 'json' || prop === 'additional') {\n return (...args: any[]) => {\n const result = value.apply(target, args)\n // Schedule checkSend after json or additional\n setImmediate(() => target['checkSend']())\n return result\n }\n } else if (prop === 'send') {\n return (...args: any[]) => {\n // Prevent checkSend from firing\n target['shouldSend'] = false\n\n return value.apply(target, args)\n }\n }\n }\n return value\n },\n })\n}\n\nexport default function BaseResource<R extends Resource> (\n evt: H3Event,\n rsc: R\n) {\n return ApiResource(new JsonResource<R>(evt, rsc))\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;ACGO,IAAeA,aAAf,MAAeA;EAAtB,OAAsBA;;;AAEtB;;;ACLA,gBAAkE;AAClE,qBAAuD;AAKhD,IAAMC,UAAN,MAAMA;EANb,OAMaA;;;;;;;;EAKAC;;;;;EAMAC;;;;;EAMAC;;;;EAKQC;EAEjB,YACIA,OAIOC,KACT;SADSA,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;","names":["Middleware","Request","params","query","headers","event","app","getQuery","getRouterParams","req","all","data","method","Object","assign","fromEntries","formData","entries","readBody","input","key","defaultValue","getEvent","safeDot","import_support","import_h3","Response","event","statusCode","headers","app","setStatusCode","code","res","status","setHeader","name","value","html","content","applyHeaders","json","data","text","redirect","url","Object","entries","forEach","key","set","getEvent","safeDot","LogRequests","Middleware","handle","request","next","url","getEvent","console","log","pathname","search","import_h3","HttpServiceProvider","ServiceProvider","priority","register","app","singleton","H3","serve","JsonResource","request","response","resource","body","data","shouldSend","responseSent","event","rsc","req","res","key","Object","keys","defineProperty","enumerable","configurable","get","set","value","json","status","Array","isArray","pagination","meta","additional","send","code","context","this","checkSend","ApiResource","instance","Proxy","get","target","prop","receiver","value","Reflect","args","result","apply","setImmediate"]}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["ServiceProvider","H3","serve","app: Application","event: H3Event","data: Resource","meta: BodyResource['meta']","#send","app: Application"],"sources":["../src/Middleware.ts","../src/Middleware/LogRequests.ts","../src/Providers/HttpServiceProvider.ts","../src/Request.ts","../src/Resources/JsonResource.ts","../src/Resources/ApiResource.ts","../src/Response.ts"],"sourcesContent":["import { HttpContext } from './Contracts/HttpContract'\nimport { IMiddleware } from '@h3ravel/shared'\n\nexport abstract class Middleware implements IMiddleware {\n abstract handle (context: HttpContext, next: () => Promise<unknown>): Promise<unknown>\n}\n","import { HttpContext } from '@h3ravel/shared'\nimport { Middleware } from '../Middleware'\n\nexport class LogRequests extends Middleware {\n async handle ({ request }: HttpContext, next: () => Promise<unknown>): Promise<unknown> {\n const url = request.getEvent('url')\n console.log(`[${request.getEvent('method')}] ${url.pathname + url.search}`)\n return next()\n }\n}\n","import { H3, serve } from 'h3'\n\nimport { ServiceProvider } from '@h3ravel/core'\n\n/**\n * Sets up HTTP kernel and request lifecycle.\n * \n * Register Request, Response, and Middleware classes.\n * Configure global middleware stack.\n * Boot HTTP kernel.\n * \n * Auto-Registered\n */\nexport class HttpServiceProvider extends ServiceProvider {\n public static priority = 998;\n\n register () {\n this.app.singleton('http.app', () => {\n return new H3()\n })\n\n this.app.singleton('http.serve', () => serve)\n }\n}\n","import { getQuery, getRouterParams, readBody, type H3Event } from 'h3'\nimport { safeDot } from '@h3ravel/support'\nimport type { DotNestedKeys, DotNestedValue } from '@h3ravel/shared'\nimport type { ResponseHeaderMap, TypedHeaders } from 'fetchdts'\nimport { IRequest } from '@h3ravel/shared'\nimport { Application } from '@h3ravel/core'\n\nexport class Request implements IRequest {\n /**\n * Gets route parameters.\n * @returns An object containing route parameters.\n */\n readonly params: NonNullable<H3Event[\"context\"][\"params\"]>\n\n /**\n * Gets query parameters.\n * @returns An object containing query parameters.\n */\n readonly query: Record<string, string>;\n\n /**\n * Gets the request headers.\n * @returns An object containing request headers.\n */\n readonly headers: TypedHeaders<Record<keyof ResponseHeaderMap, string>>\n\n /**\n * The current H3 H3Event instance\n */\n private readonly event: H3Event\n\n constructor(\n event: H3Event,\n /**\n * The current app instance\n */\n public app: Application\n ) {\n this.event = event\n this.query = getQuery(this.event)\n this.params = getRouterParams(this.event)\n this.headers = this.event.req.headers\n }\n\n /**\n * Get all input data (query + body).\n */\n async all<T = Record<string, unknown>> (): Promise<T> {\n let data = {\n ...getRouterParams(this.event),\n ...getQuery(this.event),\n } as T\n\n if (this.event.req.method === 'POST') {\n data = Object.assign({}, data, Object.fromEntries((await this.event.req.formData()).entries()))\n } else if (this.event.req.method === 'PUT') {\n data = <never>Object.fromEntries(Object.entries(<never>await readBody(this.event)))\n }\n\n return data\n }\n\n /**\n * Get a single input field from query or body.\n */\n async input<T = unknown> (key: string, defaultValue?: T): Promise<T> {\n const data = await this.all<Record<string, T>>()\n return (data[key] ?? defaultValue) as T\n }\n\n /**\n * Get the base event\n */\n getEvent (): H3Event\n getEvent<K extends DotNestedKeys<H3Event>> (key: K): DotNestedValue<H3Event, K>\n getEvent<K extends DotNestedKeys<H3Event>> (key?: K): any {\n return safeDot(this.event, key)\n }\n}\n","import { EventHandlerRequest, H3Event } from 'h3'\n\nexport interface Resource {\n [key: string]: any;\n pagination?: {\n from?: number | undefined;\n to?: number | undefined;\n perPage?: number | undefined;\n total?: number | undefined;\n } | undefined;\n}\n\ntype BodyResource = Resource & {\n data: Omit<Resource, 'pagination'>,\n meta?: {\n pagination?: Resource['pagination']\n } | undefined;\n}\n\n/**\n * Class to render API resource\n */\nexport class JsonResource<R extends Resource = any> {\n /**\n * The request instance\n */\n request: H3Event<EventHandlerRequest>['req']\n /**\n * The response instance\n */\n response: H3Event['res']\n /**\n * The data to send to the client\n */\n resource: R\n /**\n * The final response data object\n */\n body: BodyResource = {\n data: {},\n }\n /**\n * Flag to track if response should be sent automatically\n */\n private shouldSend: boolean = false\n /**\n * Flag to track if response has been sent\n */\n\n private responseSent: boolean = false;\n\n /**\n * Declare that this includes R's properties\n */\n [key: string]: any;\n\n /**\n * @param req The request instance\n * @param res The response instance\n * @param rsc The data to send to the client\n */\n constructor(protected event: H3Event, rsc: R) {\n this.request = event.req\n this.response = event.res\n this.resource = rsc\n\n // Copy all properties from rsc to this, avoiding conflicts\n for (const key of Object.keys(rsc)) {\n if (!(key in this)) {\n Object.defineProperty(this, key, {\n enumerable: true,\n configurable: true,\n get: () => this.resource[key],\n set: (value) => {\n (<any>this.resource)[key] = value\n },\n })\n }\n }\n }\n\n /**\n * Return the data in the expected format\n * \n * @returns \n */\n data (): Resource {\n return this.resource\n }\n\n /**\n * Build the response object\n * @returns this\n */\n json () {\n // Indicate response should be sent automatically\n this.shouldSend = true\n\n // Set default status code\n this.response.status = 200\n\n // Prepare body\n const resource = this.data()\n let data: Resource = Array.isArray(resource) ? [...resource] : { ...resource }\n\n if (typeof data.data !== 'undefined') {\n data = data.data\n }\n\n if (!Array.isArray(resource)) {\n delete data.pagination\n }\n\n this.body = {\n data,\n }\n\n // Set the pagination from the data() resource, if available\n if (!Array.isArray(resource) && resource.pagination) {\n const meta: BodyResource['meta'] = this.body.meta ?? {}\n meta.pagination = resource.pagination\n this.body.meta = meta\n }\n\n // If pagination is not available on the resource, then check and set it\n // if it's available on the base resource.\n if (this.resource.pagination && !this.body.meta?.pagination) {\n const meta: BodyResource['meta'] = this.body.meta ?? {}\n meta.pagination = this.resource.pagination\n this.body.meta = meta\n }\n\n return this\n }\n\n /**\n * Add context data to the response object\n * @param data Context data\n * @returns this\n */\n additional<X extends { [key: string]: any }> (data: X) {\n\n // Allow automatic send after additional\n this.shouldSend = true\n\n // Merge data with body\n delete data.data\n delete data.pagination\n\n this.body = {\n ...this.body,\n ...data,\n }\n\n return this\n }\n\n /**\n * Send the output to the client\n * @returns this\n */\n send () {\n this.shouldSend = false // Prevent automatic send\n if (!this.responseSent) {\n this.#send()\n }\n return this\n }\n\n /**\n * Set the status code for this response\n * @param code Status code\n * @returns this\n */\n status (code: number) {\n this.response.status = code\n return this\n }\n\n /**\n * Private method to send the response\n */\n #send () {\n if (!this.responseSent) {\n this.event.context.\n this.response.json(this.body)\n\n // Mark response as sent\n this.responseSent = true\n }\n }\n\n /**\n * Check if send should be triggered automatically\n */\n private checkSend () {\n if (this.shouldSend && !this.responseSent) {\n this.#send()\n }\n }\n}\n","import { JsonResource, Resource } from './JsonResource'\n\nimport { H3Event } from 'h3'\n\nexport function ApiResource (\n instance: JsonResource\n) {\n return new Proxy(instance, {\n get (target, prop, receiver) {\n const value = Reflect.get(target, prop, receiver)\n if (typeof value === 'function') {\n // Intercept json, additional, and send methods\n if (prop === 'json' || prop === 'additional') {\n return (...args: any[]) => {\n const result = value.apply(target, args)\n // Schedule checkSend after json or additional\n setImmediate(() => target['checkSend']())\n return result\n }\n } else if (prop === 'send') {\n return (...args: any[]) => {\n // Prevent checkSend from firing\n target['shouldSend'] = false\n\n return value.apply(target, args)\n }\n }\n }\n return value\n },\n })\n}\n\nexport default function BaseResource<R extends Resource> (\n evt: H3Event,\n rsc: R\n) {\n return ApiResource(new JsonResource<R>(evt, rsc))\n}\n","import type { DotNestedKeys, DotNestedValue } from '@h3ravel/shared'\nimport { html, redirect, } from 'h3'\n\nimport { Application } from '@h3ravel/core'\nimport type { H3Event } from 'h3'\nimport { IResponse } from '@h3ravel/shared'\nimport { safeDot } from '@h3ravel/support'\n\nexport class Response implements IResponse {\n /**\n * The current H3 H3Event instance\n */\n private readonly event: H3Event\n\n private statusCode: number = 200\n private headers: Record<string, string> = {}\n\n constructor(\n event: H3Event,\n /**\n * The current app instance\n */\n public app: Application\n ) {\n this.event = event\n }\n\n /**\n * Set HTTP status code.\n */\n setStatusCode (code: number): this {\n this.statusCode = code\n this.event.res.status = code\n return this\n }\n\n /**\n * Set a header.\n */\n setHeader (name: string, value: string): this {\n this.headers[name] = value\n return this\n }\n\n html (content: string): string {\n this.applyHeaders()\n return html(this.event, content)\n }\n\n /**\n * Send a JSON response.\n */\n json<T = unknown> (data: T): T {\n this.setHeader('content-type', 'application/json; charset=utf-8')\n this.applyHeaders()\n return data\n }\n\n /**\n * Send plain text.\n */\n text (data: string): string {\n this.setHeader('content-type', 'text/plain; charset=utf-8')\n this.applyHeaders()\n return data\n }\n\n /**\n * Redirect to another URL.\n */\n redirect (url: string, status = 302): string {\n this.setStatusCode(status)\n return redirect(this.event, url, this.statusCode)\n }\n\n /**\n * Apply headers before sending response.\n */\n private applyHeaders (): void {\n Object.entries(this.headers).forEach(([key, value]) => {\n this.event.res.headers.set(key, value)\n })\n }\n\n /**\n * Get the base event\n */\n getEvent (): H3Event\n getEvent<K extends DotNestedKeys<H3Event>> (key: K): DotNestedValue<H3Event, K>\n getEvent<K extends DotNestedKeys<H3Event>> (key?: K): any {\n return safeDot(this.event, key)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,IAAsB,aAAtB,MAAwD;;;;ACAxD,IAAa,cAAb,cAAiC,WAAW;CACxC,MAAM,OAAQ,EAAE,WAAwB,MAAgD;EACpF,MAAM,MAAM,QAAQ,SAAS,MAAM;AACnC,UAAQ,IAAI,IAAI,QAAQ,SAAS,SAAS,CAAC,IAAI,IAAI,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AAO;AAC1B,0BAAgB,KAAK,OAAO,KAAK,KAAK,WAAW;;;;;CAMrD,AAAQ,eAAsB;AAC1B,SAAO,QAAQ,KAAK,QAAQ,CAAC,SAAS,CAAC,KAAK,WAAW;AACnD,QAAK,MAAM,IAAI,QAAQ,IAAI,KAAK,MAAM;IACxC;;CAQN,SAA4C,KAAc;AACtD,wCAAe,KAAK,OAAO,IAAI"}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,110 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { TypedHeaders, ResponseHeaderMap } from 'fetchdts';
|
|
6
|
-
import { Application, ServiceProvider } from '@h3ravel/core';
|
|
1
|
+
import { DotNestedKeys, DotNestedValue, HttpContext, HttpContext as HttpContext$1, IMiddleware, IRequest, IResponse } from "@h3ravel/shared";
|
|
2
|
+
import { Application, ServiceProvider } from "@h3ravel/core";
|
|
3
|
+
import { EventHandlerRequest, H3Event } from "h3";
|
|
4
|
+
import { ResponseHeaderMap, TypedHeaders } from "fetchdts";
|
|
7
5
|
|
|
6
|
+
//#region src/Middleware.d.ts
|
|
8
7
|
declare abstract class Middleware implements IMiddleware {
|
|
9
|
-
|
|
8
|
+
abstract handle(context: HttpContext, next: () => Promise<unknown>): Promise<unknown>;
|
|
10
9
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* The current app instance
|
|
15
|
-
*/
|
|
16
|
-
app: Application;
|
|
17
|
-
/**
|
|
18
|
-
* Gets route parameters.
|
|
19
|
-
* @returns An object containing route parameters.
|
|
20
|
-
*/
|
|
21
|
-
readonly params: NonNullable<H3Event["context"]["params"]>;
|
|
22
|
-
/**
|
|
23
|
-
* Gets query parameters.
|
|
24
|
-
* @returns An object containing query parameters.
|
|
25
|
-
*/
|
|
26
|
-
readonly query: Record<string, string>;
|
|
27
|
-
/**
|
|
28
|
-
* Gets the request headers.
|
|
29
|
-
* @returns An object containing request headers.
|
|
30
|
-
*/
|
|
31
|
-
readonly headers: TypedHeaders<Record<keyof ResponseHeaderMap, string>>;
|
|
32
|
-
/**
|
|
33
|
-
* The current H3 H3Event instance
|
|
34
|
-
*/
|
|
35
|
-
private readonly event;
|
|
36
|
-
constructor(event: H3Event,
|
|
37
|
-
/**
|
|
38
|
-
* The current app instance
|
|
39
|
-
*/
|
|
40
|
-
app: Application);
|
|
41
|
-
/**
|
|
42
|
-
* Get all input data (query + body).
|
|
43
|
-
*/
|
|
44
|
-
all<T = Record<string, unknown>>(): Promise<T>;
|
|
45
|
-
/**
|
|
46
|
-
* Get a single input field from query or body.
|
|
47
|
-
*/
|
|
48
|
-
input<T = unknown>(key: string, defaultValue?: T): Promise<T>;
|
|
49
|
-
/**
|
|
50
|
-
* Get the base event
|
|
51
|
-
*/
|
|
52
|
-
getEvent(): H3Event;
|
|
53
|
-
getEvent<K extends DotNestedKeys<H3Event>>(key: K): DotNestedValue<H3Event, K>;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
declare class Response implements IResponse {
|
|
57
|
-
/**
|
|
58
|
-
* The current app instance
|
|
59
|
-
*/
|
|
60
|
-
app: Application;
|
|
61
|
-
/**
|
|
62
|
-
* The current H3 H3Event instance
|
|
63
|
-
*/
|
|
64
|
-
private readonly event;
|
|
65
|
-
private statusCode;
|
|
66
|
-
private headers;
|
|
67
|
-
constructor(event: H3Event,
|
|
68
|
-
/**
|
|
69
|
-
* The current app instance
|
|
70
|
-
*/
|
|
71
|
-
app: Application);
|
|
72
|
-
/**
|
|
73
|
-
* Set HTTP status code.
|
|
74
|
-
*/
|
|
75
|
-
setStatusCode(code: number): this;
|
|
76
|
-
/**
|
|
77
|
-
* Set a header.
|
|
78
|
-
*/
|
|
79
|
-
setHeader(name: string, value: string): this;
|
|
80
|
-
html(content: string): string;
|
|
81
|
-
/**
|
|
82
|
-
* Send a JSON response.
|
|
83
|
-
*/
|
|
84
|
-
json<T = unknown>(data: T): T;
|
|
85
|
-
/**
|
|
86
|
-
* Send plain text.
|
|
87
|
-
*/
|
|
88
|
-
text(data: string): string;
|
|
89
|
-
/**
|
|
90
|
-
* Redirect to another URL.
|
|
91
|
-
*/
|
|
92
|
-
redirect(url: string, status?: number): string;
|
|
93
|
-
/**
|
|
94
|
-
* Apply headers before sending response.
|
|
95
|
-
*/
|
|
96
|
-
private applyHeaders;
|
|
97
|
-
/**
|
|
98
|
-
* Get the base event
|
|
99
|
-
*/
|
|
100
|
-
getEvent(): H3Event;
|
|
101
|
-
getEvent<K extends DotNestedKeys<H3Event>>(key: K): DotNestedValue<H3Event, K>;
|
|
102
|
-
}
|
|
103
|
-
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/Middleware/LogRequests.d.ts
|
|
104
12
|
declare class LogRequests extends Middleware {
|
|
105
|
-
|
|
13
|
+
handle({
|
|
14
|
+
request
|
|
15
|
+
}: HttpContext$1, next: () => Promise<unknown>): Promise<unknown>;
|
|
106
16
|
}
|
|
107
|
-
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/Providers/HttpServiceProvider.d.ts
|
|
108
19
|
/**
|
|
109
20
|
* Sets up HTTP kernel and request lifecycle.
|
|
110
21
|
*
|
|
@@ -115,101 +26,198 @@ declare class LogRequests extends Middleware {
|
|
|
115
26
|
* Auto-Registered
|
|
116
27
|
*/
|
|
117
28
|
declare class HttpServiceProvider extends ServiceProvider {
|
|
118
|
-
|
|
119
|
-
|
|
29
|
+
static priority: number;
|
|
30
|
+
register(): void;
|
|
120
31
|
}
|
|
121
|
-
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/Request.d.ts
|
|
34
|
+
declare class Request implements IRequest {
|
|
35
|
+
/**
|
|
36
|
+
* The current app instance
|
|
37
|
+
*/
|
|
38
|
+
app: Application;
|
|
39
|
+
/**
|
|
40
|
+
* Gets route parameters.
|
|
41
|
+
* @returns An object containing route parameters.
|
|
42
|
+
*/
|
|
43
|
+
readonly params: NonNullable<H3Event["context"]["params"]>;
|
|
44
|
+
/**
|
|
45
|
+
* Gets query parameters.
|
|
46
|
+
* @returns An object containing query parameters.
|
|
47
|
+
*/
|
|
48
|
+
readonly query: Record<string, string>;
|
|
49
|
+
/**
|
|
50
|
+
* Gets the request headers.
|
|
51
|
+
* @returns An object containing request headers.
|
|
52
|
+
*/
|
|
53
|
+
readonly headers: TypedHeaders<Record<keyof ResponseHeaderMap, string>>;
|
|
54
|
+
/**
|
|
55
|
+
* The current H3 H3Event instance
|
|
56
|
+
*/
|
|
57
|
+
private readonly event;
|
|
58
|
+
constructor(event: H3Event,
|
|
59
|
+
/**
|
|
60
|
+
* The current app instance
|
|
61
|
+
*/
|
|
62
|
+
app: Application);
|
|
63
|
+
/**
|
|
64
|
+
* Get all input data (query + body).
|
|
65
|
+
*/
|
|
66
|
+
all<T = Record<string, unknown>>(): Promise<T>;
|
|
67
|
+
/**
|
|
68
|
+
* Get a single input field from query or body.
|
|
69
|
+
*/
|
|
70
|
+
input<T = unknown>(key: string, defaultValue?: T): Promise<T>;
|
|
71
|
+
/**
|
|
72
|
+
* Get the base event
|
|
73
|
+
*/
|
|
74
|
+
getEvent(): H3Event;
|
|
75
|
+
getEvent<K extends DotNestedKeys<H3Event>>(key: K): DotNestedValue<H3Event, K>;
|
|
76
|
+
}
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/Resources/JsonResource.d.ts
|
|
122
79
|
interface Resource {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
80
|
+
[key: string]: any;
|
|
81
|
+
pagination?: {
|
|
82
|
+
from?: number | undefined;
|
|
83
|
+
to?: number | undefined;
|
|
84
|
+
perPage?: number | undefined;
|
|
85
|
+
total?: number | undefined;
|
|
86
|
+
} | undefined;
|
|
130
87
|
}
|
|
131
88
|
type BodyResource = Resource & {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
89
|
+
data: Omit<Resource, 'pagination'>;
|
|
90
|
+
meta?: {
|
|
91
|
+
pagination?: Resource['pagination'];
|
|
92
|
+
} | undefined;
|
|
136
93
|
};
|
|
137
94
|
/**
|
|
138
95
|
* Class to render API resource
|
|
139
96
|
*/
|
|
140
97
|
declare class JsonResource<R extends Resource = any> {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
98
|
+
#private;
|
|
99
|
+
protected event: H3Event;
|
|
100
|
+
/**
|
|
101
|
+
* The request instance
|
|
102
|
+
*/
|
|
103
|
+
request: H3Event<EventHandlerRequest>['req'];
|
|
104
|
+
/**
|
|
105
|
+
* The response instance
|
|
106
|
+
*/
|
|
107
|
+
response: H3Event['res'];
|
|
108
|
+
/**
|
|
109
|
+
* The data to send to the client
|
|
110
|
+
*/
|
|
111
|
+
resource: R;
|
|
112
|
+
/**
|
|
113
|
+
* The final response data object
|
|
114
|
+
*/
|
|
115
|
+
body: BodyResource;
|
|
116
|
+
/**
|
|
117
|
+
* Flag to track if response should be sent automatically
|
|
118
|
+
*/
|
|
119
|
+
private shouldSend;
|
|
120
|
+
/**
|
|
121
|
+
* Flag to track if response has been sent
|
|
122
|
+
*/
|
|
123
|
+
private responseSent;
|
|
124
|
+
/**
|
|
125
|
+
* Declare that this includes R's properties
|
|
126
|
+
*/
|
|
127
|
+
[key: string]: any;
|
|
128
|
+
/**
|
|
129
|
+
* @param req The request instance
|
|
130
|
+
* @param res The response instance
|
|
131
|
+
* @param rsc The data to send to the client
|
|
132
|
+
*/
|
|
133
|
+
constructor(event: H3Event, rsc: R);
|
|
134
|
+
/**
|
|
135
|
+
* Return the data in the expected format
|
|
136
|
+
*
|
|
137
|
+
* @returns
|
|
138
|
+
*/
|
|
139
|
+
data(): Resource;
|
|
140
|
+
/**
|
|
141
|
+
* Build the response object
|
|
142
|
+
* @returns this
|
|
143
|
+
*/
|
|
144
|
+
json(): this;
|
|
145
|
+
/**
|
|
146
|
+
* Add context data to the response object
|
|
147
|
+
* @param data Context data
|
|
148
|
+
* @returns this
|
|
149
|
+
*/
|
|
150
|
+
additional<X extends {
|
|
170
151
|
[key: string]: any;
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
json(): this;
|
|
188
|
-
/**
|
|
189
|
-
* Add context data to the response object
|
|
190
|
-
* @param data Context data
|
|
191
|
-
* @returns this
|
|
192
|
-
*/
|
|
193
|
-
additional<X extends {
|
|
194
|
-
[key: string]: any;
|
|
195
|
-
}>(data: X): this;
|
|
196
|
-
/**
|
|
197
|
-
* Send the output to the client
|
|
198
|
-
* @returns this
|
|
199
|
-
*/
|
|
200
|
-
send(): this;
|
|
201
|
-
/**
|
|
202
|
-
* Set the status code for this response
|
|
203
|
-
* @param code Status code
|
|
204
|
-
* @returns this
|
|
205
|
-
*/
|
|
206
|
-
status(code: number): this;
|
|
207
|
-
/**
|
|
208
|
-
* Check if send should be triggered automatically
|
|
209
|
-
*/
|
|
210
|
-
private checkSend;
|
|
152
|
+
}>(data: X): this;
|
|
153
|
+
/**
|
|
154
|
+
* Send the output to the client
|
|
155
|
+
* @returns this
|
|
156
|
+
*/
|
|
157
|
+
send(): this;
|
|
158
|
+
/**
|
|
159
|
+
* Set the status code for this response
|
|
160
|
+
* @param code Status code
|
|
161
|
+
* @returns this
|
|
162
|
+
*/
|
|
163
|
+
status(code: number): this;
|
|
164
|
+
/**
|
|
165
|
+
* Check if send should be triggered automatically
|
|
166
|
+
*/
|
|
167
|
+
private checkSend;
|
|
211
168
|
}
|
|
212
|
-
|
|
169
|
+
//#endregion
|
|
170
|
+
//#region src/Resources/ApiResource.d.ts
|
|
213
171
|
declare function ApiResource(instance: JsonResource): JsonResource<any>;
|
|
214
|
-
|
|
215
|
-
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/Response.d.ts
|
|
174
|
+
declare class Response implements IResponse {
|
|
175
|
+
/**
|
|
176
|
+
* The current app instance
|
|
177
|
+
*/
|
|
178
|
+
app: Application;
|
|
179
|
+
/**
|
|
180
|
+
* The current H3 H3Event instance
|
|
181
|
+
*/
|
|
182
|
+
private readonly event;
|
|
183
|
+
private statusCode;
|
|
184
|
+
private headers;
|
|
185
|
+
constructor(event: H3Event,
|
|
186
|
+
/**
|
|
187
|
+
* The current app instance
|
|
188
|
+
*/
|
|
189
|
+
app: Application);
|
|
190
|
+
/**
|
|
191
|
+
* Set HTTP status code.
|
|
192
|
+
*/
|
|
193
|
+
setStatusCode(code: number): this;
|
|
194
|
+
/**
|
|
195
|
+
* Set a header.
|
|
196
|
+
*/
|
|
197
|
+
setHeader(name: string, value: string): this;
|
|
198
|
+
html(content: string): string;
|
|
199
|
+
/**
|
|
200
|
+
* Send a JSON response.
|
|
201
|
+
*/
|
|
202
|
+
json<T = unknown>(data: T): T;
|
|
203
|
+
/**
|
|
204
|
+
* Send plain text.
|
|
205
|
+
*/
|
|
206
|
+
text(data: string): string;
|
|
207
|
+
/**
|
|
208
|
+
* Redirect to another URL.
|
|
209
|
+
*/
|
|
210
|
+
redirect(url: string, status?: number): string;
|
|
211
|
+
/**
|
|
212
|
+
* Apply headers before sending response.
|
|
213
|
+
*/
|
|
214
|
+
private applyHeaders;
|
|
215
|
+
/**
|
|
216
|
+
* Get the base event
|
|
217
|
+
*/
|
|
218
|
+
getEvent(): H3Event;
|
|
219
|
+
getEvent<K extends DotNestedKeys<H3Event>>(key: K): DotNestedValue<H3Event, K>;
|
|
220
|
+
}
|
|
221
|
+
//#endregion
|
|
222
|
+
export { ApiResource, HttpContext, HttpServiceProvider, JsonResource, LogRequests, Middleware, Request, Resource, Response };
|
|
223
|
+
//# sourceMappingURL=index.d.cts.map
|