Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
feat(core): per-collection cache/cors option (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
boywithkeyboard committed Jun 14, 2023
1 parent da87926 commit dd26429
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,48 @@ import { Handler, Route } from './Handler.d.ts'
import { ObjectSchema, Schema } from './validator/Validator.d.ts'

export class Collection {
#cache:
| false
| {
maxAge: number
}
| undefined

#cors:
| string
| undefined

routes: [
string,
string,
Route[]
][]

constructor() {
constructor({
cache,
cors
}: {
/**
* Duration in seconds for how long a response should be cached.
*
* @since v0.11
*/
cache?:
| false
| {
maxAge: number
}
/**
* Enable Cross-Origin Resource Sharing (CORS) for this collection by setting a origin, e.g. `*`.
*
* @since v0.11
*/
cors?: string
} = {}) {
this.routes = []

this.#cache = cache
this.#cors = cors
}

/* Get Method --------------------------------------------------------------- */
Expand Down Expand Up @@ -66,6 +100,14 @@ export class Collection {
>
)[]
) {
if ((this.#cache || this.#cors) && typeof handler[0] !== 'function') {
if (handler[0].cache === undefined && this.#cache !== undefined)
handler[0].cache = this.#cache

if (!handler[0].cors)
handler[0].cors = this.#cors
}

this.routes.push([ 'GET', url, handler ])

return this
Expand Down Expand Up @@ -129,6 +171,9 @@ export class Collection {
>
)[]
) {
if (this.#cors && typeof handler[0] !== 'function' && !handler[0].cors)
handler[0].cors = this.#cors

this.routes.push([ 'DELETE', url, handler ])

return this
Expand Down Expand Up @@ -192,6 +237,9 @@ export class Collection {
>
)[]
) {
if (this.#cors && typeof handler[0] !== 'function' && !handler[0].cors)
handler[0].cors = this.#cors

this.routes.push([ 'POST', url, handler ])

return this
Expand Down Expand Up @@ -255,6 +303,9 @@ export class Collection {
>
)[]
) {
if (this.#cors && typeof handler[0] !== 'function' && !handler[0].cors)
handler[0].cors = this.#cors

this.routes.push([ 'PUT', url, handler ])

return this
Expand Down Expand Up @@ -318,6 +369,9 @@ export class Collection {
>
)[]
) {
if (this.#cors && typeof handler[0] !== 'function' && !handler[0].cors)
handler[0].cors = this.#cors

this.routes.push([ 'PATCH', url, handler ])

return this
Expand Down Expand Up @@ -375,6 +429,9 @@ export class Collection {
>
)[]
) {
if (this.#cors && typeof handler[0] !== 'function' && !handler[0].cors)
handler[0].cors = this.#cors

this.routes.push([ 'HEAD', url, handler ])

return this
Expand Down

0 comments on commit dd26429

Please sign in to comment.