1 |
|
2 |
|
3 | import Err from '@lskjs/err';
|
4 | import asyncMapValues from '@lskjs/utils/asyncMapValues';
|
5 | import Bluebird from 'bluebird';
|
6 |
|
7 | import Api from './Api';
|
8 |
|
9 | export const collectConfigs = async (m) => ({
|
10 | config: m.config,
|
11 | modules: await asyncMapValues(m.__availableModules, async (_, name) => collectConfigs(await m.module(name))),
|
12 | });
|
13 |
|
14 | export const collectConfigsLog = async (m) => ({
|
15 | debug: m.config.debug,
|
16 | log: m.config.log,
|
17 | modules: await asyncMapValues(m.__availableModules, async (_, name) => collectConfigsLog(await m.module(name))),
|
18 | });
|
19 |
|
20 | const middleware = (req, res, next) => {
|
21 | req.file = '[some-file]';
|
22 | if (Math.random() < 0.5) {
|
23 | console.log('test.error');
|
24 | next(new Error('test.error'));
|
25 | } else {
|
26 | next();
|
27 | }
|
28 | };
|
29 |
|
30 | export class TestApi extends Api {
|
31 | getRoutes() {
|
32 | return {
|
33 | ...super.getRoutes(),
|
34 | '/middleware/1': async (req, res) => {
|
35 | await this.useMiddleware(middleware, req, res);
|
36 | console.log('req.file', req.file);
|
37 | return { file: req.file };
|
38 | },
|
39 | '/middleware/2': [
|
40 | middleware,
|
41 | async (req, res) => {
|
42 | console.log('req.file', req.file);
|
43 | return { file: req.file };
|
44 | },
|
45 | ],
|
46 | '/res/1': () => 123,
|
47 | '/res/2': () => 'Hello',
|
48 | '/res/3': (req, res) => res.send('Hello'),
|
49 | '/res/4': () => ({ test: 123 }),
|
50 | '/res/5': () => [1, 2, 3, 4],
|
51 | '/res/6': () => true,
|
52 | '/res/7': () => null,
|
53 | '/res/8': () => undefined,
|
54 | '/res/9': () => () => {},
|
55 | '/res/10': () => /[0-9]ig/,
|
56 | '/res/11': () => ({ __raw: '<html />' }),
|
57 | '/res/12': () => ({ __pack: true, some: 123, help: 'me' }),
|
58 | '/res/13': () =>
|
59 | function some() {
|
60 | const secret = 123;
|
61 | return secret;
|
62 | },
|
63 | '/async/ok': async () => {
|
64 | const delay = 5000;
|
65 | await Bluebird.delay(delay);
|
66 | return { delay };
|
67 | },
|
68 | '/async/err': async () => {
|
69 | const delay = 5000;
|
70 | await Bluebird.delay(delay);
|
71 | throw new Err('ERR_QWE', { delay });
|
72 | },
|
73 | '/async/404': async () => {
|
74 | const delay = 5000;
|
75 | await Bluebird.delay(delay);
|
76 | throw new Err('ERR_QWE', { delay, status: 404 });
|
77 | },
|
78 | '/async/502': async () => {
|
79 | const delay = 5000;
|
80 | await Bluebird.delay(delay);
|
81 | throw new Err('ERR_QWE', { delay, status: 502 });
|
82 | },
|
83 | '/err/1': () => {
|
84 | throw new Err('TEST_ERROR_CODE');
|
85 | },
|
86 | '/err/2': () => {
|
87 | throw { code: 'TEST_ERROR_CODE', message: 'The message text' };
|
88 | },
|
89 | '/err/3': () => {
|
90 | throw new Error('The message text');
|
91 | },
|
92 | '/err/4': () => {
|
93 | throw new Error('TEST_ERROR_CODE');
|
94 | },
|
95 | '/err/5': () => {
|
96 | const error = new Error('The message text');
|
97 | error.code = 'TEST_ERROR_CODE';
|
98 | throw new Error('The message text');
|
99 | },
|
100 | '/err/6': () => {
|
101 | throw {};
|
102 | },
|
103 | '/err/7': () => {
|
104 | throw null;
|
105 | },
|
106 | '/err/10': () => {
|
107 | throw new Err();
|
108 | },
|
109 | '/err/11': () => {
|
110 | throw new Err('TEST_ERROR_CODE');
|
111 | },
|
112 | '/err/12': () => {
|
113 | throw new Err('TEST_ERROR_CODE', { message: 'The message text' });
|
114 | },
|
115 | '/err/13': () => {
|
116 | throw new Err('TEST_ERROR_CODE', { message: 'The message text' }, { status: 404 });
|
117 | },
|
118 | '/err/14': () => {
|
119 | throw new Err({ code: 'TEST_ERROR_CODE', message: 'The message text' }, { status: 404 });
|
120 | },
|
121 | '/err/15': () => {
|
122 | throw new Err('test.someError', { status: 404 });
|
123 | },
|
124 | '/err/16': () => {
|
125 | throw new Err('test.anotherError', { status: 404, data: { hello: 'world' } });
|
126 | },
|
127 | '/err/17': () => {
|
128 | throw new Error('err', 'file', 123);
|
129 | },
|
130 | '/err/18': () => {
|
131 | throw new Err({ code: 'TEST_ERROR_CODE', message: 'The message text' }, { status: 404 });
|
132 | },
|
133 | '/err/19': () => {
|
134 | throw new Err({ code: 'TEST_ERROR_CODE', message: 'The message text' }, { status: 404 });
|
135 | },
|
136 | '/err/20': () => {
|
137 | throw new Err({ code: 'TEST_ERROR_CODE', message: 'The message text' }, { status: 404 });
|
138 | },
|
139 | '/err/21': () => {
|
140 | throw new Err('user.notFound', { status: 404 });
|
141 | },
|
142 | '/err/22': () => {
|
143 | throw new Err('some.error', { status: 404 });
|
144 | },
|
145 | '/err/24': () => {
|
146 | throw new Err('some.error', { status: 404 });
|
147 | },
|
148 | '/form': (req) => {
|
149 | const { email, password } = req.data;
|
150 | if (password !== 'password') throw new Err('auth.passwordInvalud', { status: 400, data: { password } });
|
151 | if (email !== 'test@coder24.ru') throw new Err('auth.emailInvalud', { status: 400, data: { email } });
|
152 | return { ok: 123 };
|
153 | },
|
154 | '/locale': (req) => {
|
155 | this.log.trace('locale start');
|
156 |
|
157 | return { locale: req.getLocale(), test: req.t('test.hello') };
|
158 | },
|
159 | '/app/config': () => collectConfigs(this.app),
|
160 | '/app/config/log': () => collectConfigsLog(this.app),
|
161 | '/req/auth': async (req) => req.user,
|
162 | '/req/user': async (req) => req.user,
|
163 | '/req/userId': async (req) => req.userId || (req.user && req.user_id),
|
164 | '/req/:log?': this.req.bind(this),
|
165 | '/user/one': async () => {
|
166 | const { UserModel } = this.app;
|
167 | const user = await UserModel.findOne();
|
168 | return user;
|
169 |
|
170 | },
|
171 | '/path/*': () => ({
|
172 | path: this.path,
|
173 | paths: this.paths,
|
174 | }),
|
175 | };
|
176 | }
|
177 |
|
178 | req(req) {
|
179 | const res = {
|
180 | params: req.params,
|
181 | query: req.query,
|
182 | headers: req.headers,
|
183 | };
|
184 | if (req.params.log) {
|
185 | this.log.info('req', res);
|
186 | res.body = req.body;
|
187 | this.log.info('body', req.body);
|
188 | }
|
189 |
|
190 | return res;
|
191 | }
|
192 | }
|
193 |
|
194 | export default TestApi;
|