insomnia/app/models/cookie-jar.js

60 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-08-22 22:30:57 +00:00
// @flow
2016-11-10 05:56:23 +00:00
import * as db from '../common/database';
2017-08-22 22:30:57 +00:00
import type {BaseModel} from './index';
2016-09-21 20:32:45 +00:00
export const name = 'Cookie Jar';
export const type = 'CookieJar';
export const prefix = 'jar';
export const canDuplicate = true;
2017-08-22 22:30:57 +00:00
export type Cookie = {
domain: string,
path: string,
key: string,
value: string,
expires: number
}
type BaseCookieJar = {
name: string,
cookies: Array<Cookie>
};
export type CookieJar = BaseModel & BaseCookieJar;
export function init () {
2016-11-10 01:15:27 +00:00
return {
name: 'Default Jar',
cookies: []
};
}
2016-09-21 20:32:45 +00:00
2017-08-22 22:30:57 +00:00
export function migrate (doc: CookieJar): CookieJar {
return doc;
}
2017-08-22 22:30:57 +00:00
export function create (patch: Object = {}) {
return db.docCreate(type, patch);
}
2016-09-21 20:32:45 +00:00
2017-08-22 22:30:57 +00:00
export async function getOrCreateForParentId (parentId: string) {
const cookieJars = await db.find(type, {parentId});
if (cookieJars.length === 0) {
return await create({parentId});
} else {
return cookieJars[0];
}
}
2016-09-21 20:32:45 +00:00
export function all () {
return db.all(type);
}
2016-09-21 20:32:45 +00:00
2017-08-22 22:30:57 +00:00
export function getById (id: string) {
return db.get(type, id);
}
2016-09-21 20:32:45 +00:00
2017-08-22 22:30:57 +00:00
export function update (cookieJar: CookieJar, patch: Object = {}) {
2016-09-21 20:32:45 +00:00
return db.docUpdate(cookieJar, patch);
}