2018-06-25 17:42:50 +00:00
|
|
|
import { PREVIEW_MODE_FRIENDLY } from '../common/constants';
|
2021-07-22 23:04:56 +00:00
|
|
|
import { database as db } from '../common/database';
|
2019-05-10 01:18:34 +00:00
|
|
|
import type { BaseModel } from './index';
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
|
|
export const name = 'Request Meta';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2016-12-01 01:50:59 +00:00
|
|
|
export const type = 'RequestMeta';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2016-12-01 01:50:59 +00:00
|
|
|
export const prefix = 'reqm';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-06-15 17:08:24 +00:00
|
|
|
export const canDuplicate = false;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
export const canSync = false;
|
2016-12-01 01:50:59 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
interface BaseRequestMeta {
|
|
|
|
parentId: string;
|
|
|
|
previewMode: string;
|
|
|
|
responseFilter: string;
|
2021-05-18 20:32:18 +00:00
|
|
|
responseFilterHistory: string[];
|
2021-05-12 06:35:00 +00:00
|
|
|
activeResponseId: string | null;
|
|
|
|
savedRequestBody: Record<string, any>;
|
|
|
|
pinned: boolean;
|
|
|
|
lastActive: number;
|
|
|
|
downloadPath: string | null;
|
|
|
|
}
|
2019-05-10 01:18:34 +00:00
|
|
|
|
|
|
|
export type RequestMeta = BaseModel & BaseRequestMeta;
|
|
|
|
|
2021-06-16 19:19:00 +00:00
|
|
|
export const isRequestMeta = (model: Pick<BaseModel, 'type'>): model is RequestMeta => (
|
|
|
|
model.type === type
|
|
|
|
);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function migrate(doc: RequestMeta) {
|
2016-12-01 01:50:59 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function create(patch: Partial<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-12-01 20:29:30 +00:00
|
|
|
// expectParentToBeRequest(patch.parentId);
|
2021-05-12 06:35:00 +00:00
|
|
|
return db.docCreate<RequestMeta>(type, patch);
|
2016-12-01 01:50:59 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function update(requestMeta: RequestMeta, patch: Partial<RequestMeta>) {
|
2020-12-01 20:29:30 +00:00
|
|
|
// expectParentToBeRequest(patch.parentId || requestMeta.parentId);
|
2021-05-12 06:35:00 +00:00
|
|
|
return db.docUpdate<RequestMeta>(requestMeta, patch);
|
2016-12-01 01:50:59 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function getByParentId(parentId: string): Promise<RequestMeta | null> {
|
2020-12-01 20:29:30 +00:00
|
|
|
// expectParentToBeRequest(parentId);
|
2021-05-12 06:35:00 +00:00
|
|
|
return db.getWhere<RequestMeta>(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
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function updateOrCreateByParentId(parentId: string, patch: Partial<RequestMeta>) {
|
2020-10-28 22:23:07 +00:00
|
|
|
const requestMeta = await getByParentId(parentId);
|
|
|
|
|
|
|
|
if (requestMeta) {
|
|
|
|
return update(requestMeta, patch);
|
|
|
|
} else {
|
2021-05-12 06:35:00 +00:00
|
|
|
const newPatch = Object.assign(
|
|
|
|
{
|
|
|
|
parentId,
|
|
|
|
},
|
|
|
|
patch,
|
|
|
|
);
|
2020-10-28 22:23:07 +00:00
|
|
|
return create(newPatch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function all() {
|
|
|
|
return db.all<RequestMeta>(type);
|
2016-12-01 01:50:59 +00:00
|
|
|
}
|
2020-11-02 00:55:22 +00:00
|
|
|
|
2020-12-01 20:29:30 +00:00
|
|
|
// TODO: Ensure the parent of RequestMeta can only be a Request - INS-341
|
|
|
|
// function expectParentToBeRequest(parentId: string) {
|
|
|
|
// if (!isRequestId(parentId)) {
|
|
|
|
// throw new Error('Expected the parent of RequestMeta to be a Request');
|
|
|
|
// }
|
|
|
|
// }
|