From a44a23750e984f3f4ad4c8ad0aae96305dade174 Mon Sep 17 00:00:00 2001 From: Eric Newton Date: Wed, 4 May 2022 15:12:25 -0400 Subject: [PATCH 1/3] ignore $schema definition --- src/cli/run.js | 3 +++ src/server/router/index.js | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/src/cli/run.js b/src/cli/run.js index 294145e33..2e45b8f84 100644 --- a/src/cli/run.js +++ b/src/cli/run.js @@ -15,6 +15,9 @@ function prettyPrint(argv, object, rules) { console.log() console.log(chalk.bold(' Resources')) for (const prop in object) { + // skip printing $schema nodes + if (prop === '$schema') continue + console.log(` ${root}/${prop}`) } diff --git a/src/server/router/index.js b/src/server/router/index.js index 8a246b1bc..6b59d8441 100644 --- a/src/server/router/index.js +++ b/src/server/router/index.js @@ -54,6 +54,11 @@ module.exports = (db, opts) => { // Create routes db.forEach((value, key) => { + if (key === '$schema') { + // ignore $schema + return + } + if (_.isPlainObject(value)) { router.use(`/${key}`, singular(db, key, opts)) return From 8af22b0d3ec7fff0ed7ee78469de1e17d28ec9d4 Mon Sep 17 00:00:00 2001 From: Eric Newton Date: Thu, 5 May 2022 10:52:40 -0400 Subject: [PATCH 2/3] added tests that verify $schema node is ignored --- __tests__/server/schema-ignore.js | 51 +++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 __tests__/server/schema-ignore.js diff --git a/__tests__/server/schema-ignore.js b/__tests__/server/schema-ignore.js new file mode 100644 index 000000000..6f8efe84c --- /dev/null +++ b/__tests__/server/schema-ignore.js @@ -0,0 +1,51 @@ +const request = require('supertest') +const jsonServer = require('../../src/server') + +describe('Schema-ignore', () => { + let server + let router + let db + + beforeEach(() => { + db = { + $schema: 'http://some.schema.somewhere/', + } + + db.user = { + name: 'foo', + email: 'foo@example.com', + } + + server = jsonServer.create() + router = jsonServer.router(db) + server.use(jsonServer.defaults()) + server.use(router) + }) + + describe('GET /:resource', () => { + test('should respond with corresponding resource', () => + request(server).get('/user').expect(200, db.user)) + }) + + describe('POST /:resource', () => { + test('should create resource', () => { + const user = { name: 'bar' } + return request(server).post('/user').send(user).expect(201, user) + }) + }) + + describe('PUT /:resource', () => { + test('should update resource', () => { + const user = { name: 'bar' } + return request(server).put('/user').send(user).expect(200, user) + }) + }) + + describe('PATCH /:resource', () => { + test('should update resource', () => + request(server) + .patch('/user') + .send({ name: 'bar' }) + .expect(200, { name: 'bar', email: 'foo@example.com' })) + }) +}) From bc934fb8b22c36546cca81956edfd544e14f9892 Mon Sep 17 00:00:00 2001 From: Eric Newton Date: Thu, 5 May 2022 12:07:21 -0400 Subject: [PATCH 3/3] reduced schema ignore tests (copied from other tests) --- __tests__/server/schema-ignore.js | 29 +++-------------------------- 1 file changed, 3 insertions(+), 26 deletions(-) diff --git a/__tests__/server/schema-ignore.js b/__tests__/server/schema-ignore.js index 6f8efe84c..8c1959d6b 100644 --- a/__tests__/server/schema-ignore.js +++ b/__tests__/server/schema-ignore.js @@ -1,7 +1,7 @@ const request = require('supertest') const jsonServer = require('../../src/server') -describe('Schema-ignore', () => { +describe('$schema-ignore', () => { let server let router let db @@ -22,30 +22,7 @@ describe('Schema-ignore', () => { server.use(router) }) - describe('GET /:resource', () => { - test('should respond with corresponding resource', () => - request(server).get('/user').expect(200, db.user)) - }) - - describe('POST /:resource', () => { - test('should create resource', () => { - const user = { name: 'bar' } - return request(server).post('/user').send(user).expect(201, user) - }) - }) - - describe('PUT /:resource', () => { - test('should update resource', () => { - const user = { name: 'bar' } - return request(server).put('/user').send(user).expect(200, user) - }) - }) - - describe('PATCH /:resource', () => { - test('should update resource', () => - request(server) - .patch('/user') - .send({ name: 'bar' }) - .expect(200, { name: 'bar', email: 'foo@example.com' })) + test('doesnt error with $schema node', () => { + return request(server).get('/user').expect(200, db.user) }) })