restful.ts 871 B

1234567891011121314151617181920212223242526
  1. import {clone, extend} from "@util/JsonToolkit";
  2. const request = require('supertest');
  3. const globalAny: any = global;
  4. export const DatetimeRegex = /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/;
  5. export const DateRegex = /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/;
  6. const json: any = {
  7. ver: '1.0',
  8. app_id: 'test-app-id',
  9. timestamp: new Date().getTime(),
  10. token: 'token',
  11. __cachedData: {},
  12. }
  13. export const api = async (path: string, data: any): Promise<any> => {
  14. try {
  15. let req = clone(json);
  16. req.__cachedData.user_id = globalAny.user_id;
  17. req = extend(req, {data: data});
  18. let response = await request(globalAny.app)
  19. .post(path).send(req);
  20. return response.body;
  21. } catch (e) {
  22. console.error('Could not request', e);
  23. }
  24. };