Skip to content

Commit

Permalink
Merge pull request #72 from bouwe77/remove-cache-control-header
Browse files Browse the repository at this point in the history
Remove cache-control header
  • Loading branch information
bouwe77 authored Aug 15, 2024
2 parents 81dbf1b + 6dd1811 commit 5479912
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 30 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ Here is an example of the config settings for Temba, and how you define them:
const config = {
allowDeleteCollection: true,
apiPrefix: 'api',
cacheControl: 'public, max-age=300',
connectionString: 'mongodb://localhost:27017/myDatabase',
customRouter: router,
delay: 500,
Expand Down Expand Up @@ -485,7 +484,6 @@ These are all the possible settings:
| :------------------------ | :----------------------------------------------------------------------------------------- | :------------ |
| `allowDeleteCollection` | Whether a `DELETE` request on a collection is allowed to delete all items. | `false` |
| `apiPrefix` | See [API prefix](#api-prefix) | `null` |
| `cacheControl` | The `Cache-control` response header value for each GET request. | `'no-store'` |
| `connectionString` | See [Data persistency](#data-persistency) | `null` |
| `customRouter` | See [Custom router](#custom-router) | `null` |
| `delay` | The delay, in milliseconds, after processing the request before sending the response. | `0` |
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "temba",
"version": "0.31.0",
"version": "0.32.0",
"description": "Get a simple REST API with zero coding in less than 30 seconds (seriously).",
"type": "module",
"main": "dist/src/index.js",
Expand Down
8 changes: 0 additions & 8 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export type Config = {
validateResources: boolean
resources: string[]
apiPrefix: string | null
cacheControl: string
requestInterceptor: RequestInterceptor | null
responseBodyInterceptor: ResponseBodyInterceptor | null
staticFolder: string | null
Expand All @@ -28,7 +27,6 @@ export type RouterConfig = Pick<
| 'validateResources'
| 'resources'
| 'apiPrefix'
| 'cacheControl'
| 'requestInterceptor'
| 'responseBodyInterceptor'
| 'returnNullFields'
Expand All @@ -40,7 +38,6 @@ export type UserConfig = {
staticFolder?: string
apiPrefix?: string
connectionString?: string
cacheControl?: string
delay?: number
requestInterceptor?: RequestInterceptor
responseBodyInterceptor?: ResponseBodyInterceptor
Expand All @@ -58,7 +55,6 @@ const defaultConfig: Config = {
staticFolder: null,
apiPrefix: null,
connectionString: null,
cacheControl: 'no-store',
delay: 0,
requestInterceptor: null,
responseBodyInterceptor: null,
Expand Down Expand Up @@ -92,10 +88,6 @@ export const initConfig = (userConfig?: UserConfig): Config => {
config.connectionString = userConfig.connectionString
}

if (userConfig.cacheControl && userConfig.cacheControl.length > 0) {
config.cacheControl = userConfig.cacheControl
}

if (
userConfig.delay &&
userConfig.delay !== 0 &&
Expand Down
6 changes: 2 additions & 4 deletions src/requestHandlers/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import { removeNullFields } from './utils'

export const createGetRoutes = (
queries: Queries,
cacheControl: string,
requestInterceptor: RequestInterceptor | null,
responseBodyInterceptor: ResponseBodyInterceptor | null,
returnNullFields: boolean,
) => {
const defaultResponse = { headers: { 'Cache-control': cacheControl } }
const responseOk = (body: unknown) => ({ ...defaultResponse, status: 200, body })
const responseOk = (body: unknown) => ({ status: 200, body })

const handleGet = async (req: GetRequest) => {
try {
Expand All @@ -36,7 +34,7 @@ export const createGetRoutes = (
const item = await queries.getById(resource, id)

if (!item) {
return { ...defaultResponse, status: 404 }
return { status: 404 }
}

const theItem = responseBodyInterceptor
Expand Down
10 changes: 2 additions & 8 deletions src/requestHandlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ export const getRequestHandler = (
schemas: CompiledSchemas,
routerConfig: RouterConfig,
) => {
const {
cacheControl,
requestInterceptor,
responseBodyInterceptor,
returnNullFields,
allowDeleteCollection,
} = routerConfig
const { requestInterceptor, responseBodyInterceptor, returnNullFields, allowDeleteCollection } =
routerConfig

const handleGet = createGetRoutes(
queries,
cacheControl,
requestInterceptor,
responseBodyInterceptor,
returnNullFields,
Expand Down
5 changes: 0 additions & 5 deletions test/unit/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const defaultConfig: Config = {
staticFolder: null,
apiPrefix: null,
connectionString: null,
cacheControl: 'no-store',
delay: 0,
requestInterceptor: null,
responseBodyInterceptor: null,
Expand All @@ -29,7 +28,6 @@ test('No config returns default config', () => {
expect(initializedConfig.staticFolder).toBe(defaultConfig.staticFolder)
expect(initializedConfig.apiPrefix).toBe(defaultConfig.apiPrefix)
expect(initializedConfig.connectionString).toBe(defaultConfig.connectionString)
expect(initializedConfig.cacheControl).toBe(defaultConfig.cacheControl)
expect(initializedConfig.delay).toBe(defaultConfig.delay)
expect(initializedConfig.requestInterceptor?.get).toBe(defaultConfig.requestInterceptor?.get)
expect(initializedConfig.requestInterceptor?.post).toBe(defaultConfig.requestInterceptor?.post)
Expand Down Expand Up @@ -58,7 +56,6 @@ test('Full user config overrides all defaults', () => {
staticFolder: 'build',
apiPrefix: 'api',
connectionString: 'mongodb://localhost:27017',
cacheControl: 'no-cache',
delay: 1000,
requestInterceptor: {
get: () => {
Expand Down Expand Up @@ -105,7 +102,6 @@ test('Full user config overrides all defaults', () => {
expect(config.staticFolder).toBe('build')
expect(config.apiPrefix).toBe('/api/')
expect(config.connectionString).toBe('mongodb://localhost:27017')
expect(config.cacheControl).toBe('no-cache')
expect(config.delay).toBe(1000)
expect(config.requestInterceptor!.get).toBeInstanceOf(Function)
expect(config.requestInterceptor!.post).toBeInstanceOf(Function)
Expand All @@ -131,7 +127,6 @@ test('Partial user config applies those, but leaves the rest at default', () =>
expect(config.staticFolder).toBe(defaultConfig.staticFolder)
expect(config.apiPrefix).toBe('/api/')
expect(config.connectionString).toBe(defaultConfig.connectionString)
expect(config.cacheControl).toBe(defaultConfig.cacheControl)
expect(config.delay).toBe(defaultConfig.delay)
expect(config.requestInterceptor?.get).toBe(defaultConfig.requestInterceptor?.get)
expect(config.requestInterceptor?.post).toBe(defaultConfig.requestInterceptor?.post)
Expand Down

0 comments on commit 5479912

Please sign in to comment.