@financial-times/dotcom-middleware-asset-loader 7.3.1 → 7.3.2
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/package.json +3 -2
- package/src/__test__/createMiddleware.spec.ts +35 -0
- package/src/__test__/index.spec.ts +42 -0
- package/src/createMiddleware.ts +12 -0
- package/src/createStaticHost.ts +8 -0
- package/src/index.ts +23 -0
- package/src/options.d.ts +9 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@financial-times/dotcom-middleware-asset-loader",
|
|
3
|
-
"version": "7.3.
|
|
3
|
+
"version": "7.3.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/node/index.js",
|
|
6
6
|
"types": "src/index.ts",
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"npm": "7.x || 8.x"
|
|
33
33
|
},
|
|
34
34
|
"files": [
|
|
35
|
-
"dist/"
|
|
35
|
+
"dist/",
|
|
36
|
+
"src/"
|
|
36
37
|
],
|
|
37
38
|
"repository": {
|
|
38
39
|
"type": "git",
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import subject from '../createMiddleware'
|
|
2
|
+
import httpMocks from 'node-mocks-http'
|
|
3
|
+
|
|
4
|
+
let instance
|
|
5
|
+
let request
|
|
6
|
+
let response
|
|
7
|
+
let next
|
|
8
|
+
|
|
9
|
+
jest.mock('@financial-times/dotcom-server-asset-loader')
|
|
10
|
+
|
|
11
|
+
describe('dotcom-middleware-asset-loader/src/createMiddleware', () => {
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
instance = subject({})
|
|
14
|
+
request = httpMocks.createRequest({ app: { locals: {} } })
|
|
15
|
+
response = httpMocks.createResponse()
|
|
16
|
+
next = jest.fn()
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
instance = null
|
|
21
|
+
request = null
|
|
22
|
+
response = null
|
|
23
|
+
next = null
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('calls the fallthrough function', () => {
|
|
27
|
+
instance(request, response, next)
|
|
28
|
+
expect(next).toHaveBeenCalled()
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('adds an instance of the asset loader to response.locals', () => {
|
|
32
|
+
instance(request, response, next)
|
|
33
|
+
expect(response.locals.assetLoader).toBeDefined()
|
|
34
|
+
})
|
|
35
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { init } from '../index'
|
|
2
|
+
|
|
3
|
+
let instanceWithStaticHost
|
|
4
|
+
let instanceNoStaticHost
|
|
5
|
+
|
|
6
|
+
jest.mock('@financial-times/dotcom-server-asset-loader')
|
|
7
|
+
|
|
8
|
+
describe('dotcom-middleware-asset-loader', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
instanceWithStaticHost = init({ hostStaticAssets: true })
|
|
11
|
+
instanceNoStaticHost = init({ hostStaticAssets: false })
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
instanceWithStaticHost = null
|
|
16
|
+
instanceNoStaticHost = null
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('returns an array of handler functions', () => {
|
|
20
|
+
expect(instanceWithStaticHost).toBeInstanceOf(Array)
|
|
21
|
+
|
|
22
|
+
instanceWithStaticHost.forEach((item) => {
|
|
23
|
+
expect(item).toBeInstanceOf(Function)
|
|
24
|
+
})
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
describe('with hostStaticAssets enabled', () => {
|
|
28
|
+
it('returns multiple handlers', () => {
|
|
29
|
+
expect(instanceWithStaticHost).toHaveLength(2)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('returns an instance of the router', () => {
|
|
33
|
+
expect(instanceWithStaticHost[1].name).toEqual('router')
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
describe('without hostStaticAssets enabled', () => {
|
|
38
|
+
it('returns one handler', () => {
|
|
39
|
+
expect(instanceNoStaticHost).toHaveLength(1)
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
})
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AssetLoader } from '@financial-times/dotcom-server-asset-loader'
|
|
2
|
+
import { MiddlewareOptions } from './options'
|
|
3
|
+
import { Handler, Request, Response, NextFunction } from 'express'
|
|
4
|
+
|
|
5
|
+
export default (options: MiddlewareOptions): Handler => {
|
|
6
|
+
const loader = new AssetLoader(options)
|
|
7
|
+
|
|
8
|
+
return (_: Request, response: Response, next: NextFunction) => {
|
|
9
|
+
response.locals.assetLoader = loader
|
|
10
|
+
next()
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import express, { Handler } from 'express'
|
|
2
|
+
import { MiddlewareOptions } from './options'
|
|
3
|
+
|
|
4
|
+
export default (options: MiddlewareOptions): Handler => {
|
|
5
|
+
const router = express.Router()
|
|
6
|
+
router.use(options.publicPath, express.static(options.fileSystemPath))
|
|
7
|
+
return router
|
|
8
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import { Handler } from 'express'
|
|
3
|
+
import { MiddlewareOptions } from './options'
|
|
4
|
+
import createMiddleware from './createMiddleware'
|
|
5
|
+
import createStaticHost from './createStaticHost'
|
|
6
|
+
|
|
7
|
+
const defaultOptions: MiddlewareOptions = {
|
|
8
|
+
hostStaticAssets: false,
|
|
9
|
+
publicPath: '/public',
|
|
10
|
+
fileSystemPath: path.resolve('./public')
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function init(userOptions: MiddlewareOptions): Handler[] {
|
|
14
|
+
const options: MiddlewareOptions = { ...defaultOptions, ...userOptions }
|
|
15
|
+
|
|
16
|
+
const stack = [createMiddleware(options)]
|
|
17
|
+
|
|
18
|
+
if (options.hostStaticAssets) {
|
|
19
|
+
stack.push(createStaticHost(options))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return stack
|
|
23
|
+
}
|
package/src/options.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AssetLoaderOptions } from '@financial-times/dotcom-server-asset-loader'
|
|
2
|
+
|
|
3
|
+
export interface MiddlewareOptions extends AssetLoaderOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Set to true if assets should be served from a local directory
|
|
6
|
+
* @default false
|
|
7
|
+
*/
|
|
8
|
+
hostStaticAssets?: boolean
|
|
9
|
+
}
|