2019-05-10 01:18:34 +00:00
|
|
|
// @flow
|
2016-12-01 01:50:59 +00:00
|
|
|
import * as db from '../common/database';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { PREVIEW_MODE_FRIENDLY } from '../common/constants';
|
2019-05-10 01:18:34 +00:00
|
|
|
import type { BaseModel } from './index';
|
2020-10-28 22:23:07 +00:00
|
|
|
import { prefix as requestPrefix } from './request';
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
|
|
export const name = 'Request Meta';
|
|
|
|
export const type = 'RequestMeta';
|
|
|
|
export const prefix = 'reqm';
|
2017-06-15 17:08:24 +00:00
|
|
|
export const canDuplicate = false;
|
2019-04-18 00:50:03 +00:00
|
|
|
export const canSync = false;
|
2016-12-01 01:50:59 +00:00
|
|
|
|
2019-05-10 01:18:34 +00:00
|
|
|
type BaseRequestMeta = {
|
|
|
|
parentId: string,
|
|
|
|
previewMode: string,
|
|
|
|
responseFilter: string,
|
|
|
|
responseFilterHistory: Array<string>,
|
|
|
|
activeResponseId: string | null,
|
|
|
|
savedRequestBody: Object,
|
|
|
|
pinned: boolean,
|
|
|
|
lastActive: number,
|
2019-06-03 18:45:22 +00:00
|
|
|
downloadPath: string | null,
|
2019-05-10 01:18:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type RequestMeta = BaseModel & BaseRequestMeta;
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function init() {
|
2016-12-01 01:50:59 +00:00
|
|
|
return {
|
|
|
|
parentId: null,
|
2017-03-30 18:52:07 +00:00
|
|
|
previewMode: PREVIEW_MODE_FRIENDLY,
|
2016-12-01 01:50:59 +00:00
|
|
|
responseFilter: '',
|
2017-06-08 02:42:16 +00:00
|
|
|
responseFilterHistory: [],
|
2018-02-16 16:24:54 +00:00
|
|
|
activeResponseId: null,
|
2018-12-12 17:36:11 +00:00
|
|
|
savedRequestBody: {},
|
2019-05-07 14:46:35 +00:00
|
|
|
pinned: false,
|
2019-05-10 01:18:34 +00:00
|
|
|
lastActive: 0,
|
2019-06-03 18:45:22 +00:00
|
|
|
downloadPath: null,
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2016-12-01 01:50:59 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 01:18:34 +00:00
|
|
|
export function migrate(doc: RequestMeta): RequestMeta {
|
2016-12-01 01:50:59 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:58:57 +00:00
|
|
|
export function create(patch: $Shape<RequestMeta> = {}) {
|
2016-12-01 01:50:59 +00:00
|
|
|
if (!patch.parentId) {
|
2019-05-10 01:18:34 +00:00
|
|
|
throw new Error('New RequestMeta missing `parentId` ' + JSON.stringify(patch));
|
2016-12-01 01:50:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 22:23:07 +00:00
|
|
|
if (!patch.parentId.startsWith(`${requestPrefix}_`)) {
|
|
|
|
throw new Error('Expected the parent of RequestMeta to be a Request');
|
|
|
|
}
|
|
|
|
|
2016-12-01 01:50:59 +00:00
|
|
|
return db.docCreate(type, patch);
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:58:57 +00:00
|
|
|
export function update(requestMeta: RequestMeta, patch: $Shape<RequestMeta>) {
|
2016-12-01 01:50:59 +00:00
|
|
|
return db.docUpdate(requestMeta, patch);
|
|
|
|
}
|
|
|
|
|
2019-05-10 01:18:34 +00:00
|
|
|
export function getByParentId(parentId: string) {
|
2018-06-25 17:42:50 +00:00
|
|
|
return db.getWhere(type, { parentId });
|
2016-12-01 01:50:59 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 01:18:34 +00:00
|
|
|
export async function getOrCreateByParentId(parentId: string) {
|
2018-03-02 09:49:10 +00:00
|
|
|
const requestMeta = await getByParentId(parentId);
|
|
|
|
|
|
|
|
if (requestMeta) {
|
|
|
|
return requestMeta;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
return create({ parentId });
|
2018-03-02 09:49:10 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 22:23:07 +00:00
|
|
|
export async function updateOrCreateByParentId(parentId: string, patch: $Shape<RequestMeta>) {
|
|
|
|
const requestMeta = await getByParentId(parentId);
|
|
|
|
|
|
|
|
if (requestMeta) {
|
|
|
|
return update(requestMeta, patch);
|
|
|
|
} else {
|
|
|
|
const newPatch = Object.assign({ parentId }, patch);
|
|
|
|
return create(newPatch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 01:18:34 +00:00
|
|
|
export function all(): Promise<Array<RequestMeta>> {
|
2016-12-01 01:50:59 +00:00
|
|
|
return db.all(type);
|
|
|
|
}
|