Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
Polish helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Fajfa committed Feb 10, 2020
1 parent b350703 commit c893893
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 86 deletions.
2 changes: 1 addition & 1 deletion src/compose/types/page-block/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface PageBlockStyleWrap {
}

interface PageBlockStyle {
variants: PageBlockStyleVariants;
variants?: PageBlockStyleVariants;
wrap?: PageBlockStyleWrap;
}

Expand Down
121 changes: 53 additions & 68 deletions src/corredor/helpers/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { extractID, genericPermissionUpdater, isFresh, Rule } from './shared'
import { Attachment } from '../../shared'
import { Compose as ComposeAPI } from '../../api-clients'
import { Namespace, Record, Module, Page } from '../../compose'
import { Namespace, Record, Module, Page, PageBlock } from '../../compose'
import { Values } from '../../compose/types/record'

const emailStyle = `
Expand Down Expand Up @@ -41,6 +41,7 @@ interface PageFilter {
}

interface PageResponse {
[_: string]: unknown;
filter: PageFilter;
set: Array<Page>;
}
Expand All @@ -55,6 +56,7 @@ interface RecordFilter {
sort?: string;}

interface RecordResponse {
[_: string]: unknown;
filter: RecordFilter;
set: Array<Record>;
}
Expand All @@ -65,23 +67,11 @@ interface ModuleFilter {
}

interface ModuleResponse {
[_: string]: unknown;
filter: ModuleFilter;
set: Array<Module>;
}


interface Mail {
to: string|string[];
subject: string;
options?: {
header?: string;
footer?: string;
style?: string;
fields?: string[]|null;
};
record?: Promise<unknown>|Record;
}

interface NamespaceFilter {
[key: string]: string|number|undefined;
query?: string;
Expand All @@ -97,19 +87,14 @@ interface NamespaceResponse {
set: Array<Namespace>;
}

interface PageBlock {
module?: Module;
interface PageBlockParams extends PageBlock {
module: Module;
index?: number;
x?: number;
y?: number;
height?: number;
width?: number;
xywh?: number[];
fields?: object;
kind?: string;
title?: string|null;
style?: object;
options?: object;
fields: object;
}


Expand Down Expand Up @@ -189,9 +174,9 @@ export class Compose {
*/
async deletePage (page: Page): Promise<unknown> {
return Promise.resolve(page).then(page => {
// if (!(page instanceof Page)) {
// throw Error('expecting Page type')
// }
if (!(page instanceof Page)) {
throw Error('expecting Page type')
}

if (!isFresh(page.pageID)) {
return this.ComposeAPI.pageDelete(page as unknown as KV)
Expand All @@ -214,14 +199,14 @@ export class Compose {

return this.resolveNamespace(ns).then(ns => {
const namespaceID = extractID(ns, 'namespaceID')
return this.ComposeAPI.pageList({ namespaceID, ...filter as object }).then(rval => {
if (!Array.isArray(rval.set) || rval.set.length === 0) {
return Promise.reject(new Error('pages not found'))
return this.ComposeAPI.pageList({ namespaceID, ...filter as object }).then(res => {
if (!Array.isArray(res.set) || res.set.length === 0) {
throw new Error('pages not found')
}

// Casting all we got to to Page
rval.set = rval.set.map(m => new Page(m))
return rval as unknown as PageResponse
res.set = res.set.map(m => new Page(m))
return res as PageResponse
})
})
}
Expand All @@ -241,7 +226,7 @@ export class Compose {
* @param {string|Namespace|Object} ns - namespace, defaults to current $namespace
* @returns {Promise<Page>}
*/
async findPageByID (page: string|Page, ns: Namespace = this.$namespace!): Promise<Page> {
async findPageByID (page: string|Page, ns: Namespace|undefined = this.$namespace): Promise<Page> {
return this.resolveNamespace(ns).then((ns) => {
const pageID = extractID(page, 'pageID')
const namespaceID = extractID(ns, 'namespaceID')
Expand Down Expand Up @@ -276,20 +261,20 @@ export class Compose {
width = 13,
kind = 'Record',
options = {},
title = null,
title = '',
style = {},
fields,
}: PageBlock): PageBlock {
}: PageBlockParams): PageBlock {
return {
kind,
title,
xywh: [x, y + height * index, width, height],
style,
options: {
...options,
fields: fields || module!.fields,
fields: fields || module.fields,
},
}
} as unknown as PageBlock
}

/**
Expand Down Expand Up @@ -464,14 +449,14 @@ export class Compose {
params = { ...params, ...filter }
}

return this.ComposeAPI.recordList(params).then(rval => {
if (!Array.isArray(rval.set) || rval.set.length === 0) {
return this.ComposeAPI.recordList(params).then(res => {
if (!Array.isArray(res.set) || res.set.length === 0) {
throw new Error('records not found')
}

// Casting all we got to to Record
rval.set = rval.set.map(m => new Record(module, m))
return rval as unknown as RecordResponse
res.set = res.set.map(m => new Record(module, m))
return res as RecordResponse
})
})
}
Expand All @@ -488,12 +473,12 @@ export class Compose {
* @returns {Promise<Record>}
*/
async findLastRecord (module: Module|undefined = this.$module): Promise<Record> {
return this.findRecords({ sort: 'recordID DESC', page: 1, perPage: 1 }, module).then(rval => {
if (!Array.isArray(rval.set) || rval.set.length === 0) {
return this.findRecords({ sort: 'recordID DESC', page: 1, perPage: 1 }, module).then(res => {
if (!Array.isArray(res.set) || res.set.length === 0) {
throw new Error('records not found')
}

return rval.set[0]
return new Record(res.set[0])
})
}

Expand All @@ -509,12 +494,12 @@ export class Compose {
* @returns {Promise<Record>}
*/
async findFirstRecord (module: Module|undefined = this.$module): Promise<Record|null> {
return this.findRecords({ sort: 'recordID ASC', page: 1, perPage: 1 }, module).then(rval => {
if (!Array.isArray(rval.set) || rval.set.length === 0) {
return this.findRecords({ sort: 'recordID ASC', page: 1, perPage: 1 }, module).then(res => {
if (!Array.isArray(res.set) || res.set.length === 0) {
throw new Error('records not found')
}

return rval.set[0]
return new Record(res.set[0])
})
}

Expand Down Expand Up @@ -622,14 +607,14 @@ export class Compose {
return this.resolveNamespace(ns).then((ns) => {
const namespaceID = extractID(ns, 'namespaceID')

return this.ComposeAPI.moduleList({ namespaceID, ...filter as object }).then(rval => {
if (!Array.isArray(rval.set) || rval.set.length === 0) {
return Promise.reject(new Error('modules not found'))
return this.ComposeAPI.moduleList({ namespaceID, ...filter as object }).then(res => {
if (!Array.isArray(res.set) || res.set.length === 0) {
throw new Error('modules not found')
}

// Casting all we got to to Module
rval.set = rval.set.map(m => new Module(m))
return rval as unknown as ModuleResponse
res.set = res.set.map(m => new Module(m))
return res as ModuleResponse
})
})
}
Expand Down Expand Up @@ -686,12 +671,12 @@ export class Compose {
async findModuleByName (name: string, ns: string|Namespace|object|undefined = this.$namespace): Promise<Module>{
return this.resolveNamespace(ns).then((ns) => {
const namespaceID = extractID(ns, 'namespaceID')
return this.ComposeAPI.moduleList({ namespaceID, name }).then(rval => {
if (!Array.isArray(rval.set) || rval.set.length === 0) {
return Promise.reject(new Error('module not found'))
return this.ComposeAPI.moduleList({ namespaceID, name }).then(res => {
if (!Array.isArray(res.set) || res.set.length === 0) {
throw new Error('module not found')
}

return new Module(rval.set[0])
return new Module(res.set[0])
})
})
}
Expand Down Expand Up @@ -719,12 +704,12 @@ export class Compose {
async findModuleByHandle (handle: string, ns: string|Namespace|object|undefined = this.$namespace): Promise<Module> {
return this.resolveNamespace(ns).then((ns) => {
const namespaceID = extractID(ns, 'namespaceID')
return this.ComposeAPI.moduleList({ namespaceID, handle }).then(rval => {
if (!Array.isArray(rval.set) || rval.set.length === 0) {
return Promise.reject(new Error('module not found'))
return this.ComposeAPI.moduleList({ namespaceID, handle }).then(res => {
if (!Array.isArray(res.set) || res.set.length === 0) {
throw new Error('module not found')
}

return new Module(rval.set[0])
return new Module(res.set[0])
})
})
}
Expand Down Expand Up @@ -787,14 +772,14 @@ export class Compose {
filter = { query: filter }
}

return this.ComposeAPI.namespaceList({ ...filter }).then(rval => {
if (!Array.isArray(rval.set) || rval.set.length === 0) {
return Promise.reject(new Error('namespaces not found'))
return this.ComposeAPI.namespaceList({ ...filter }).then(res => {
if (!Array.isArray(res.set) || res.set.length === 0) {
throw new Error('namespaces not found')
}

// Casting all we got to to Namespace
rval.set = rval.set.map(m => new Namespace(m))
return rval as NamespaceResponse
res.set = res.set.map(m => new Namespace(m))
return res as NamespaceResponse
})
}

Expand Down Expand Up @@ -837,12 +822,12 @@ export class Compose {
* @returns {Promise<Namespace>}
*/
async findNamespaceBySlug (slug: string): Promise<Namespace> {
return this.ComposeAPI.namespaceList({ slug }).then(rval => {
if (!Array.isArray(rval.set) || rval.set.length === 0) {
return Promise.reject(new Error('namespace not found'))
return this.ComposeAPI.namespaceList({ slug }).then(res => {
if (!Array.isArray(res.set) || res.set.length === 0) {
throw new Error('namespace not found')
}

return new Namespace(rval.set[0])
return new Namespace(res.set[0])
})
}

Expand All @@ -859,7 +844,7 @@ export class Compose {
* @param {string|string[]} Any additional addresses we want this to be sent to (carbon-copy)
* @returns {Promise<void>}
*/
async sendMail (to: string|string[], subject: string, { html = '' }: { html?: string } = {}, { cc = [] }: { cc?: string|string[] } = {} ): Promise<unknown> {
async sendMail (to: string|string[], subject: string, { html = '' }: { html?: string } = {}, { cc = [] }: { cc?: string|string[] } = {} ): Promise<KV> {
if (!to) {
throw Error('expecting to email address')
}
Expand Down
7 changes: 4 additions & 3 deletions src/corredor/helpers/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface ChannelFilter {
}

interface ChannelResponse {
[_: string]: unknown;
filter: ChannelFilter;
set: Array<Channel>;
}
Expand Down Expand Up @@ -79,7 +80,7 @@ export class Messaging {
}

message.channelID = ch.channelID
return this.MessagingAPI.messageCreate(message)
return this.MessagingAPI.messageCreate(message).then(m => new Message(m))
})
}

Expand All @@ -96,11 +97,11 @@ export class Messaging {

return this.MessagingAPI.channelList(filter).then(res => {
if (!Array.isArray(res.set) || res.set.length === 0) {
return Promise.reject(new Error('channel not found'))
throw new Error('channel not found')
}

res.set = res.set.map(m => new Channel(m))
return res as unknown as ChannelResponse
return res as ChannelResponse
})
}

Expand Down
Loading

0 comments on commit c893893

Please sign in to comment.