refactor (middleware-cors): Bump to 2.0.0
MODIFY
packages/@integration/package.json
packages/@integration/package.json
"@marblejs/middleware-body": "^2.0.0",
"@marblejs/middleware-io": "^2.0.0",
"@marblejs/middleware-logger": "^2.0.0",
+ "@marblejs/middleware-cors": "^2.0.0",
"@marblejs/websockets": "^2.0.0",
"@types/node": "^10.1.2",
"rxjs": "~6.3.3",

MODIFY
packages/@integration/test/integration.spec.ts
packages/@integration/test/integration.spec.ts
@@ -119,7 +119,7 @@ describe('API integration', () => {
.expect('Content-Type', ContentType.IMAGE_PNG));
test(`OPTIONS returns 204`, async () =>
- request(app)
+ request(server)
.options('/api/v2')
.set('origin', 'fake-origin')
.expect('Access-Control-Allow-Methods', 'HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS')
@@ -130,7 +130,7 @@ describe('API integration', () => {
.expect(204));
test(`GET returns 200`, async () =>
- request(app)
+ request(server)
.get('/api/v2')
.set('origin', 'fake-origin')
.expect('Access-Control-Allow-Origin', 'fake-origin')

MODIFY
packages/middleware-cors/package.json
packages/middleware-cors/package.json
{
"name": "@marblejs/middleware-cors",
- "version": "1.2.1",
+ "version": "2.0.0",
"description": "A CORS middleware for Marble.js",
"main": "dist/index.js",
"types": "./dist/index.d.ts",
},
"homepage": "https://github.com/marblejs/marble#readme",
"peerDependencies": {
- "@marblejs/core": "^1.1.0",
+ "@marblejs/core": "^2.0.0",
"rxjs": "~6.2.2"
},
"devDependencies": {
- "@marblejs/core": "^1.2.1"
+ "@marblejs/core": "^2.0.0"
},
"publishConfig": {
"access": "public"

MODIFY
packages/middleware-cors/src/middleware.ts
packages/middleware-cors/src/middleware.ts
-import { HttpMethod, HttpStatus, Middleware } from '@marblejs/core';
+import {
+ HttpMethod,
+ HttpMiddlewareEffect,
+ HttpRequest,
+ HttpStatus,
+} from '@marblejs/core';
+import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { isString } from 'util';
import { configurePreflightResponse } from './configurePreflightResponse';
import { configureResponse } from './configureResponse';
-
export interface CORSOptions {
origin?: string | string[] | RegExp;
methods?: HttpMethod[];
@@ -23,7 +28,10 @@ const DEFAULT_OPTIONS: CORSOptions = {
optionsSuccessStatus: HttpStatus.NO_CONTENT,
};
-export const cors$ = (options: CORSOptions = {}): Middleware => (req$, res) => {
+export const cors$ = (options: CORSOptions = {}): HttpMiddlewareEffect => (
+ req$: Observable<HttpRequest>,
+ res,
+) => {
options = { ...DEFAULT_OPTIONS, ...options };
return req$.pipe(

MODIFY
packages/middleware-cors/src/spec/middleware.spec.ts
packages/middleware-cors/src/spec/middleware.spec.ts
-import { HttpMethod, HttpRequest, HttpResponse } from '@marblejs/core';
+import { HttpMethod, HttpRequest, HttpResponse, createEffectMetadata, lookup, createContext } from '@marblejs/core';
import { Marbles } from '@marblejs/core/dist/+internal';
import { cors$, CORSOptions } from '../middleware';
@@ -21,6 +21,9 @@ export const createMockRequest = (
headers: { ...headers },
} as unknown) as HttpRequest);
+const context = createContext();
+const metadata = createEffectMetadata({ ask: lookup(context) });
+
describe('CORS middleware', () => {
test('pass through non CORS requests', () => {
const request = createMockRequest('OPTIONS', { origin: null });
@@ -43,7 +46,7 @@ describe('CORS middleware', () => {
const middleware$ = cors$();
- middleware$(request$, response, undefined).subscribe(() => {
+ middleware$(request$, response, metadata).subscribe(() => {
expect(response.setHeader).toBeCalledWith('Access-Control-Allow-Origin', 'fake-origin');
done();
});
@@ -67,7 +70,7 @@ describe('CORS middleware', () => {
const middleware$ = cors$(options);
- middleware$(request$, response, undefined).subscribe(() => {
+ middleware$(request$, response, metadata).subscribe(() => {
expect(response.setHeader).toBeCalledWith('Access-Control-Allow-Origin', 'fake-origin');
expect(response.setHeader).toBeCalledWith('Access-Control-Allow-Credentials', 'true');
expect(response.setHeader).toBeCalledWith('Access-Control-Expose-Headers', 'X-Header, X-Custom-Header');

refactor (middleware-cors): Bump to 2.0.0