forked from keystonejs/keystone
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out appVersion support into a separate package (keystonejs#2535)
- Loading branch information
Showing
11 changed files
with
156 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@keystonejs/app-version': major | ||
'@keystonejs/keystone': patch | ||
--- | ||
|
||
The new package `@keystonejs/app-version` consolidates the express middleware and graphQL provider support for returning the `appVersion`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
**/*.md | ||
**/*.test.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @keystonejs/app-version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<!--[meta] | ||
section: api | ||
subSection: utilities | ||
title: App Version Plugin | ||
[meta]--> | ||
|
||
# App Version Plugin | ||
|
||
This package provides support for including a version string both as an HTTP response header and as a graphQL query. | ||
|
||
The function `appVersionMiddleware(version)` will return a piece of middleware which will set the `X-Keystone-App-Version` response header to `version` on all HTTP requests. | ||
|
||
The graphQL provider `AppVersionProvider` will add an `{ appVersion }` query to your graphQL API which returns `version` as a string. | ||
|
||
## Usage | ||
|
||
This package is designed to be used indirectly via the conveniance API on the `Keystone` class: | ||
|
||
```javascript | ||
const keystone = new Keystone({ ..., appVersion: { version: '1.0.0', addVersionToHttpHeaders: true, access: true } }); | ||
``` | ||
|
||
It can also be used directly if you would like to manually manage your middleware stack of graphQL providers. | ||
|
||
```javascript | ||
const { AppVersionProvider, appVersionMiddleware } = require('@keystonejs/app-version'); | ||
|
||
... | ||
|
||
const version = '1.0.0'; | ||
app.use(appVersionMiddleware(version)); | ||
|
||
keystone._providers.push(new AppVersionProvider({ version, access: true, schemaNames: ['public'] })); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const { AppVersionProvider, appVersionMiddleware } = require('./lib/app-version'); | ||
|
||
module.exports = { AppVersionProvider, appVersionMiddleware }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "@keystonejs/app-version", | ||
"description": "KeystoneJS App Version Plugin", | ||
"version": "0.0.0", | ||
"author": "The KeystoneJS Development Team", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=10.0.0" | ||
}, | ||
"dependencies": { | ||
"@keystonejs/access-control": "^5.2.0" | ||
}, | ||
"repository": "https://github.com/keystonejs/keystone/tree/master/packages/app-version" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const { AppVersionProvider, appVersionMiddleware } = require('../lib/app-version'); | ||
|
||
describe('AppVersionProvider', () => { | ||
test('AppVersionProvider - simple access', async () => { | ||
const appVersion = new AppVersionProvider({ | ||
version: '1.0.0', | ||
access: true, | ||
schemaNames: ['public'], | ||
}); | ||
|
||
let schemaName = 'public'; | ||
expect(appVersion.getTypes({ schemaName })).toEqual([]); | ||
expect(appVersion.getQueries({ schemaName })).toEqual([ | ||
`"""The version of the Keystone application serving this API.""" | ||
appVersion: String`, | ||
]); | ||
expect(appVersion.getMutations({ schemaName })).toEqual([]); | ||
expect(appVersion.getTypeResolvers({ schemaName })).toEqual({}); | ||
expect(appVersion.getQueryResolvers({ schemaName })).toHaveProperty('appVersion'); | ||
expect(appVersion.getQueryResolvers({ schemaName }).appVersion()).toEqual('1.0.0'); | ||
expect(appVersion.getMutationResolvers({ schemaName })).toEqual({}); | ||
|
||
schemaName = 'other'; | ||
expect(appVersion.getTypes({ schemaName })).toEqual([]); | ||
expect(appVersion.getQueries({ schemaName })).toEqual([]); | ||
expect(appVersion.getMutations({ schemaName })).toEqual([]); | ||
expect(appVersion.getTypeResolvers({ schemaName })).toEqual({}); | ||
expect(appVersion.getQueryResolvers({ schemaName })).toEqual({}); | ||
expect(appVersion.getMutationResolvers({ schemaName })).toEqual({}); | ||
}); | ||
|
||
test('AppVersionProvider - complex access', async () => { | ||
const appVersion = new AppVersionProvider({ | ||
version: '1.0.0', | ||
access: { public: true, other: false }, | ||
schemaNames: ['public', 'other'], | ||
}); | ||
|
||
let schemaName = 'public'; | ||
expect(appVersion.getTypes({ schemaName })).toEqual([]); | ||
expect(appVersion.getQueries({ schemaName })).toEqual([ | ||
`"""The version of the Keystone application serving this API.""" | ||
appVersion: String`, | ||
]); | ||
expect(appVersion.getMutations({ schemaName })).toEqual([]); | ||
expect(appVersion.getTypeResolvers({ schemaName })).toEqual({}); | ||
expect(appVersion.getQueryResolvers({ schemaName })).toHaveProperty('appVersion'); | ||
expect(appVersion.getQueryResolvers({ schemaName }).appVersion()).toEqual('1.0.0'); | ||
expect(appVersion.getMutationResolvers({ schemaName })).toEqual({}); | ||
|
||
schemaName = 'other'; | ||
expect(appVersion.getTypes({ schemaName })).toEqual([]); | ||
expect(appVersion.getQueries({ schemaName })).toEqual([]); | ||
expect(appVersion.getMutations({ schemaName })).toEqual([]); | ||
expect(appVersion.getTypeResolvers({ schemaName })).toEqual({}); | ||
expect(appVersion.getQueryResolvers({ schemaName })).toEqual({}); | ||
expect(appVersion.getMutationResolvers({ schemaName })).toEqual({}); | ||
}); | ||
}); | ||
|
||
describe('appVersionMiddleware', () => { | ||
test('appVersionMiddleware', async () => { | ||
const middleware = appVersionMiddleware('1.0.0'); | ||
|
||
const req = {}; | ||
const res = { set: jest.fn() }; | ||
const next = jest.fn(); | ||
|
||
middleware(req, res, next); | ||
expect(res.set).toHaveBeenCalledWith('X-Keystone-App-Version', '1.0.0'); | ||
expect(next).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters