insomnia/app/models/cookie-jar.js
2017-08-22 16:54:31 -07:00

62 lines
1.2 KiB
JavaScript

// @flow
import * as db from '../common/database';
import type {BaseModel} from './index';
export const name = 'Cookie Jar';
export const type = 'CookieJar';
export const prefix = 'jar';
export const canDuplicate = true;
export type Cookie = {
domain: string,
path: string,
key: string,
value: string,
expires: number,
httpOnly: boolean,
secure: boolean
}
type BaseCookieJar = {
name: string,
cookies: Array<Cookie>
};
export type CookieJar = BaseModel & BaseCookieJar;
export function init () {
return {
name: 'Default Jar',
cookies: []
};
}
export function migrate (doc: CookieJar): CookieJar {
return doc;
}
export function create (patch: Object = {}) {
return db.docCreate(type, patch);
}
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];
}
}
export function all () {
return db.all(type);
}
export function getById (id: string) {
return db.get(type, id);
}
export function update (cookieJar: CookieJar, patch: Object = {}) {
return db.docUpdate(cookieJar, patch);
}