Skip to content

Commit 62a0291

Browse files
committed
feat: refactor to ensure models on request
1 parent a359e0e commit 62a0291

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

src/file.http.router.js

+28-11
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,16 @@ import {
102102
deleteFor,
103103
Router,
104104
} from '@lykmapipo/express-rest-actions';
105-
import File from './file.model';
105+
import { createModels } from './file.model';
106106

107107
/* constants */
108108
const API_VERSION = getString('API_VERSION', '1.0.0');
109109
const PATH_SINGLE = '/files/:bucket/:id';
110110
const PATH_LIST = '/files/:bucket';
111-
const PATH_SCHEMA = '/files/:bucket/schema/';
111+
const PATH_SCHEMA = '/files/:bucket/schema';
112112

113113
/* declarations */
114-
const router = new Router({
115-
version: API_VERSION,
116-
});
114+
const router = new Router({ version: API_VERSION });
117115

118116
/**
119117
* @api {get} /files/:bucket List Files
@@ -134,7 +132,10 @@ const router = new Router({
134132
router.get(
135133
PATH_LIST,
136134
getFor({
137-
get: (options, done) => File.get(options, done),
135+
get: (options, done) => {
136+
const { File } = createModels();
137+
return File.get(options, done);
138+
},
138139
})
139140
);
140141

@@ -150,6 +151,7 @@ router.get(
150151
PATH_SCHEMA,
151152
schemaFor({
152153
getSchema: (query, done) => {
154+
const { File } = createModels();
153155
const jsonSchema = File.jsonSchema();
154156
return done(null, jsonSchema);
155157
},
@@ -175,7 +177,10 @@ router.get(
175177
router.post(
176178
PATH_LIST,
177179
postFor({
178-
post: (body, done) => File.post(body, done),
180+
post: (body, done) => {
181+
const { File } = createModels();
182+
File.post(body, done);
183+
},
179184
})
180185
);
181186

@@ -197,7 +202,10 @@ router.post(
197202
router.get(
198203
PATH_SINGLE,
199204
getByIdFor({
200-
getById: (options, done) => File.getById(options, done),
205+
getById: (options, done) => {
206+
const { File } = createModels();
207+
return File.getById(options, done);
208+
},
201209
})
202210
);
203211

@@ -220,7 +228,10 @@ router.get(
220228
router.patch(
221229
PATH_SINGLE,
222230
patchFor({
223-
patch: (options, done) => File.patch(options, done),
231+
patch: (options, done) => {
232+
const { File } = createModels();
233+
return File.patch(options, done);
234+
},
224235
})
225236
);
226237

@@ -243,7 +254,10 @@ router.patch(
243254
router.put(
244255
PATH_SINGLE,
245256
putFor({
246-
put: (options, done) => File.put(options, done),
257+
put: (options, done) => {
258+
const { File } = createModels();
259+
return File.put(options, done);
260+
},
247261
})
248262
);
249263

@@ -266,8 +280,11 @@ router.put(
266280
router.delete(
267281
PATH_SINGLE,
268282
deleteFor({
269-
del: (options, done) => File.del(options, done),
270283
soft: true,
284+
del: (options, done) => {
285+
const { File } = createModels();
286+
return File.del(options, done);
287+
},
271288
})
272289
);
273290

test/file.http.spec.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,28 @@ import {
44
} from '@lykmapipo/mongoose-test-helpers';
55
import {
66
clear as clearHttp,
7+
testRouter,
78
testUpload,
89
} from '@lykmapipo/express-test-helpers';
10+
import fileRouter from '../src/file.http.router';
911

10-
describe.only('File - API', () => {
12+
describe.only('HTTP API', () => {
1113
before(() => clearHttp());
1214
before(done => clearDatabase(done));
1315

14-
expect(clearDatabase).to.exist;
16+
const options = {
17+
pathSingle: '/files/:bucket/:id',
18+
pathList: '/files/:bucket',
19+
pathSchema: '/files/:bucket/schema',
20+
};
21+
1522
expect(testUpload).to.exist;
1623

24+
it('should handle HTTP GET on /files/:bucket/schema', done => {
25+
const { testGetSchema } = testRouter(options, fileRouter);
26+
testGetSchema({ bucket: 'files' }).expect(200, done);
27+
});
28+
1729
after(() => clearHttp());
1830
after(done => clearDatabase(done));
1931
});

0 commit comments

Comments
 (0)