Comparing version 0.1.4 to 0.1.5
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var IOEither_1 = require("fp-ts/lib/IOEither"); | ||
var Option_1 = require("fp-ts/lib/Option"); | ||
var utils_1 = require("./utils"); | ||
var auth = __non_webpack_require__("/lib/xp/auth"); | ||
function login(params) { | ||
return IOEither_1.tryCatch(function () { return auth.login(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return auth.login(params); }); | ||
} | ||
exports.login = login; | ||
function logout() { | ||
return IOEither_1.tryCatch(function () { return auth.logout(); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return auth.logout(); }); | ||
} | ||
@@ -23,19 +23,19 @@ exports.logout = logout; | ||
function findUsers(params) { | ||
return IOEither_1.tryCatch(function () { return auth.findUsers(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return auth.findUsers(params); }); | ||
} | ||
exports.findUsers = findUsers; | ||
function modifyUser(params) { | ||
return IOEither_1.tryCatch(function () { return auth.modifyUser(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return auth.modifyUser(params); }); | ||
} | ||
exports.modifyUser = modifyUser; | ||
function createUser(params) { | ||
return IOEither_1.tryCatch(function () { return auth.createUser(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return auth.createUser(params); }); | ||
} | ||
exports.createUser = createUser; | ||
function addMembers(principalKey, members) { | ||
return IOEither_1.tryCatch(function () { return auth.addMembers(principalKey, members); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return auth.addMembers(principalKey, members); }); | ||
} | ||
exports.addMembers = addMembers; | ||
function removeMembers(principalKey, members) { | ||
return IOEither_1.tryCatch(function () { return auth.removeMembers(principalKey, members); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return auth.removeMembers(principalKey, members); }); | ||
} | ||
@@ -48,12 +48,12 @@ exports.removeMembers = removeMembers; | ||
function createRole(params) { | ||
return IOEither_1.tryCatch(function () { return auth.createRole(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return auth.createRole(params); }); | ||
} | ||
exports.createRole = createRole; | ||
function createGroup(params) { | ||
return IOEither_1.tryCatch(function () { return auth.createGroup(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return auth.createGroup(params); }); | ||
} | ||
exports.createGroup = createGroup; | ||
function getMemberships(principalKey, transitive) { | ||
return IOEither_1.tryCatch(function () { return auth.getMemberships(principalKey, transitive); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return auth.getMemberships(principalKey, transitive); }); | ||
} | ||
exports.getMemberships = getMemberships; |
@@ -28,3 +28,7 @@ export declare interface Request { | ||
} | ||
export declare type EnonicErrorKey = "BadRequestError" | "UnauthorizedError" | "ForbiddenError" | "NotFoundError" | "MethodNotAllowedError" | "InternalServerError" | "BadGatewayError" | "PublishError"; | ||
interface BaseError { | ||
errorKey: EnonicErrorKey; | ||
} | ||
export declare type GeneralEnonicErrorKey = "UnauthorizedError" | "ForbiddenError" | "NotFoundError" | "MethodNotAllowedError" | "InternalServerError" | "BadGatewayError" | "PublishError"; | ||
export declare type EnonicErrorKey = GeneralEnonicErrorKey | "BadRequestError"; | ||
export declare interface Response { | ||
@@ -45,5 +49,16 @@ status: number; | ||
} | ||
export interface EnonicError { | ||
errorKey: EnonicErrorKey; | ||
export interface BadRequestErrorsByKey { | ||
[key: string]: Array<string>; | ||
} | ||
export interface GenericError extends BaseError { | ||
errorKey: GeneralEnonicErrorKey; | ||
cause?: string; | ||
stackTrace?: string; | ||
} | ||
export interface BadRequestError extends BaseError { | ||
errorKey: "BadRequestError"; | ||
errors: BadRequestErrorsByKey; | ||
} | ||
export declare function isBadRequestError(err: EnonicError): err is BadRequestError; | ||
export declare type EnonicError = GenericError | BadRequestError; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function isBadRequestError(err) { | ||
return err.errorKey === "BadRequestError"; | ||
} | ||
exports.isBadRequestError = isBadRequestError; |
import { IOEither } from "fp-ts/lib/IOEither"; | ||
import { EnonicError } from "./common"; | ||
export interface Content<T> { | ||
export interface Content<A> { | ||
_id: string; | ||
@@ -18,3 +18,3 @@ _name: string; | ||
childOrder: string; | ||
data: T; | ||
data: A; | ||
x: { | ||
@@ -45,6 +45,6 @@ [key: string]: string; | ||
} | ||
export interface QueryResponse<T> { | ||
export interface QueryResponse<A> { | ||
aggregations: object; | ||
count: number; | ||
hits: Array<Content<T>>; | ||
hits: Array<Content<A>>; | ||
total: number; | ||
@@ -58,3 +58,3 @@ } | ||
} | ||
export interface CreateContentParams<T> { | ||
export interface CreateContentParams<A> { | ||
name: string; | ||
@@ -68,8 +68,8 @@ parentPath: string; | ||
childOrder?: string; | ||
data: T; | ||
data: A; | ||
x?: string; | ||
} | ||
export interface ModifyContentParams<T> { | ||
export interface ModifyContentParams<A> { | ||
key: string; | ||
editor: (c: Content<T>) => Content<T>; | ||
editor: (c: Content<A>) => Content<A>; | ||
requireValid?: boolean; | ||
@@ -110,3 +110,3 @@ } | ||
} | ||
export interface Site<T> { | ||
export interface Site<A> { | ||
_id: string; | ||
@@ -119,3 +119,3 @@ _name: string; | ||
data: { | ||
siteConfig: SiteConfig<T>; | ||
siteConfig: SiteConfig<A>; | ||
}; | ||
@@ -129,5 +129,5 @@ x: { | ||
} | ||
export interface SiteConfig<T> { | ||
export interface SiteConfig<A> { | ||
applicationKey: string; | ||
config: T; | ||
config: A; | ||
} | ||
@@ -189,14 +189,14 @@ export interface GetSiteConfigParams { | ||
} | ||
export declare function get<T>(params: GetContentParams): IOEither<EnonicError, Content<T>>; | ||
export declare function query<T>(params: QueryContentParams): IOEither<EnonicError, QueryResponse<T>>; | ||
export declare function create<T>(params: CreateContentParams<T>): IOEither<EnonicError, Content<T>>; | ||
export declare function modify<T>(params: ModifyContentParams<T>): IOEither<EnonicError, Content<T>>; | ||
export declare function get<A>(params: GetContentParams): IOEither<EnonicError, Content<A>>; | ||
export declare function query<A>(params: QueryContentParams): IOEither<EnonicError, QueryResponse<A>>; | ||
export declare function create<A>(params: CreateContentParams<A>): IOEither<EnonicError, Content<A>>; | ||
export declare function modify<A>(params: ModifyContentParams<A>): IOEither<EnonicError, Content<A>>; | ||
export declare function remove(params: DeleteContentParams): IOEither<EnonicError, void>; | ||
export declare function publish(params: PublishContentParams): IOEither<EnonicError, PublishResponse>; | ||
export declare function unpublish(params: UnpublishContentParams): IOEither<EnonicError, Array<string>>; | ||
export declare function getChildren<T>(params: GetChildrenParams): IOEither<EnonicError, QueryResponse<T>>; | ||
export declare function move<T>(params: MoveParams): IOEither<EnonicError, Content<T>>; | ||
export declare function getSite<T>(params: GetSiteParams): IOEither<EnonicError, Site<T>>; | ||
export declare function getSiteConfig<T>(params: GetSiteConfigParams): IOEither<EnonicError, T>; | ||
export declare function createMedia<T>(params: CreateMediaParams): IOEither<EnonicError, Content<T>>; | ||
export declare function getChildren<A>(params: GetChildrenParams): IOEither<EnonicError, QueryResponse<A>>; | ||
export declare function move<A>(params: MoveParams): IOEither<EnonicError, Content<A>>; | ||
export declare function getSite<A>(params: GetSiteParams): IOEither<EnonicError, Site<A>>; | ||
export declare function getSiteConfig<A>(params: GetSiteConfigParams): IOEither<EnonicError, A>; | ||
export declare function createMedia<A>(params: CreateMediaParams): IOEither<EnonicError, Content<A>>; | ||
export declare function getAttachments(key: string): IOEither<EnonicError, Attachments>; | ||
@@ -203,0 +203,0 @@ export declare function getAttachmentStream(params: AttachmentStreamParams): IOEither<EnonicError, any>; |
@@ -6,22 +6,27 @@ "use strict"; | ||
var utils_1 = require("./utils"); | ||
var utils_2 = require("./utils"); | ||
var content = __non_webpack_require__("/lib/xp/content"); | ||
function get(params) { | ||
return pipeable_1.pipe(IOEither_1.tryCatch(function () { return content.get(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }), IOEither_1.chain(utils_1.fromNullable({ errorKey: "NotFoundError" }))); | ||
return pipeable_1.pipe(utils_2.catchEnonicError(function () { return content.get(params); }), IOEither_1.chain(utils_1.fromNullable({ errorKey: "NotFoundError" }))); | ||
} | ||
exports.get = get; | ||
function query(params) { | ||
return IOEither_1.tryCatch(function () { return content.query(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_2.catchEnonicError(function () { return content.query(params); }); | ||
} | ||
exports.query = query; | ||
function create(params) { | ||
return IOEither_1.tryCatch(function () { return content.create(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_2.catchEnonicError(function () { return content.create(params); }); | ||
} | ||
exports.create = create; | ||
function modify(params) { | ||
return IOEither_1.tryCatch(function () { return content.modify(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_2.catchEnonicError(function () { return content.modify(params); }); | ||
} | ||
exports.modify = modify; | ||
function remove(params) { | ||
return pipeable_1.pipe(IOEither_1.tryCatch(function () { return content.delete(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }), IOEither_1.chain(function (success) { | ||
return success ? IOEither_1.right(undefined) : IOEither_1.left({ errorKey: "NotFoundError" }); | ||
return pipeable_1.pipe(utils_2.catchEnonicError(function () { return content.delete(params); }), IOEither_1.chain(function (success) { | ||
return success | ||
? IOEither_1.right(undefined) | ||
: IOEither_1.left({ | ||
errorKey: "NotFoundError" | ||
}); | ||
})); | ||
@@ -31,31 +36,31 @@ } | ||
function publish(params) { | ||
return IOEither_1.tryCatch(function () { return content.publish(params); }, function (e) { return ({ errorKey: "PublishError", cause: String(e) }); }); | ||
return utils_2.catchEnonicError(function () { return content.publish(params); }, "PublishError"); | ||
} | ||
exports.publish = publish; | ||
function unpublish(params) { | ||
return IOEither_1.tryCatch(function () { return content.unpublish(params); }, function (e) { return ({ errorKey: "PublishError", cause: String(e) }); }); | ||
return utils_2.catchEnonicError(function () { return content.unpublish(params); }, "PublishError"); | ||
} | ||
exports.unpublish = unpublish; | ||
function getChildren(params) { | ||
return IOEither_1.tryCatch(function () { return content.getChildren(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_2.catchEnonicError(function () { return content.getChildren(params); }); | ||
} | ||
exports.getChildren = getChildren; | ||
function move(params) { | ||
return IOEither_1.tryCatch(function () { return content.move(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_2.catchEnonicError(function () { return content.move(params); }); | ||
} | ||
exports.move = move; | ||
function getSite(params) { | ||
return IOEither_1.tryCatch(function () { return content.getSite(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_2.catchEnonicError(function () { return content.getSite(params); }); | ||
} | ||
exports.getSite = getSite; | ||
function getSiteConfig(params) { | ||
return IOEither_1.tryCatch(function () { return content.getSiteConfig(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_2.catchEnonicError(function () { return content.getSiteConfig(params); }); | ||
} | ||
exports.getSiteConfig = getSiteConfig; | ||
function createMedia(params) { | ||
return IOEither_1.tryCatch(function () { return content.createMedia(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_2.catchEnonicError(function () { return content.createMedia(params); }); | ||
} | ||
exports.createMedia = createMedia; | ||
function getAttachments(key) { | ||
return pipeable_1.pipe(IOEither_1.tryCatch(function () { return content.getAttachments(key); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }), IOEither_1.chain(utils_1.fromNullable({ errorKey: "NotFoundError" }))); | ||
return pipeable_1.pipe(utils_2.catchEnonicError(function () { return content.getAttachments(key); }), IOEither_1.chain(utils_1.fromNullable({ errorKey: "NotFoundError" }))); | ||
} | ||
@@ -65,24 +70,24 @@ exports.getAttachments = getAttachments; | ||
function getAttachmentStream(params) { | ||
return pipeable_1.pipe(IOEither_1.tryCatch(function () { return content.getAttachmentStream(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }), IOEither_1.chain(utils_1.fromNullable({ errorKey: "NotFoundError" }))); | ||
return pipeable_1.pipe(utils_2.catchEnonicError(function () { return content.getAttachmentStream(params); }), IOEither_1.chain(utils_1.fromNullable({ errorKey: "NotFoundError" }))); | ||
} | ||
exports.getAttachmentStream = getAttachmentStream; | ||
function removeAttachment(params) { | ||
return IOEither_1.tryCatch(function () { return content.removeAttachment(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_2.catchEnonicError(function () { return content.removeAttachment(params); }); | ||
} | ||
exports.removeAttachment = removeAttachment; | ||
function getPermissions(params) { | ||
return IOEither_1.tryCatch(function () { return content.getPermissions(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_2.catchEnonicError(function () { return content.getPermissions(params); }); | ||
} | ||
exports.getPermissions = getPermissions; | ||
function setPermissions(params) { | ||
return IOEither_1.tryCatch(function () { return content.setPermissions(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_2.catchEnonicError(function () { return content.setPermissions(params); }); | ||
} | ||
exports.setPermissions = setPermissions; | ||
function getType(name) { | ||
return IOEither_1.tryCatch(function () { return content.getType(name); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_2.catchEnonicError(function () { return content.getType(name); }); | ||
} | ||
exports.getType = getType; | ||
function getTypes() { | ||
return IOEither_1.tryCatch(function () { return content.getTypes(); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_2.catchEnonicError(function () { return content.getTypes(); }); | ||
} | ||
exports.getTypes = getTypes; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var IOEither_1 = require("fp-ts/lib/IOEither"); | ||
var utils_1 = require("./utils"); | ||
var context = __non_webpack_require__("/lib/xp/context"); | ||
function get() { | ||
return IOEither_1.tryCatch(function () { return context.get(); }, function (e) { return ({ | ||
cause: String(e), | ||
errorKey: "InternalServerError" | ||
}); }); | ||
return utils_1.catchEnonicError(function () { return context.get(); }); | ||
} | ||
@@ -11,0 +8,0 @@ exports.get = get; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var IOEither_1 = require("fp-ts/lib/IOEither"); | ||
var utils_1 = require("./utils"); | ||
var httpClient = __non_webpack_require__("/lib/http-client"); | ||
@@ -10,7 +10,4 @@ /** | ||
function request(params) { | ||
return IOEither_1.tryCatch(function () { return httpClient.request(params); }, function (e) { return ({ | ||
cause: String(e), | ||
errorKey: "BadGatewayError" | ||
}); }); | ||
return utils_1.catchEnonicError(function () { return httpClient.request(params); }, "BadGatewayError"); | ||
} | ||
exports.request = request; |
@@ -5,11 +5,11 @@ "use strict"; | ||
var pipeable_1 = require("fp-ts/lib/pipeable"); | ||
var utils_1 = require("./utils"); | ||
var email = __non_webpack_require__("/lib/xp/mail"); | ||
function send(params) { | ||
return pipeable_1.pipe(IOEither_1.tryCatch(function () { return email.send(params); }, function (e) { return ({ | ||
cause: String(e), | ||
errorKey: "InternalServerError" | ||
}); }), IOEither_1.chain(function (success) { | ||
return success ? IOEither_1.right(undefined) : IOEither_1.left({ errorKey: "InternalServerError" }); | ||
return pipeable_1.pipe(utils_1.catchEnonicError(function () { return email.send(params); }), IOEither_1.chain(function (success) { | ||
return success | ||
? IOEither_1.right(undefined) | ||
: IOEither_1.left({ errorKey: "InternalServerError" }); | ||
})); | ||
} | ||
exports.send = send; |
@@ -5,2 +5,3 @@ "use strict"; | ||
var pipeable_1 = require("fp-ts/lib/pipeable"); | ||
var utils_1 = require("./utils"); | ||
var node = __non_webpack_require__("/lib/xp/node"); | ||
@@ -11,3 +12,3 @@ /** | ||
function connect(params) { | ||
return IOEither_1.tryCatch(function () { return node.connect(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return node.connect(params); }); | ||
} | ||
@@ -19,3 +20,3 @@ exports.connect = connect; | ||
function get(repo, keys) { | ||
return pipeable_1.pipe(IOEither_1.tryCatch(function () { return repo.get(keys); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }), IOEither_1.map(function (data) { return (Array.isArray(data) ? data : [data]); })); | ||
return pipeable_1.pipe(utils_1.catchEnonicError(function () { return repo.get(keys); }), IOEither_1.map(function (data) { return (Array.isArray(data) ? data : [data]); })); | ||
} | ||
@@ -27,3 +28,3 @@ exports.get = get; | ||
function create(repo, params) { | ||
return IOEither_1.tryCatch(function () { return repo.create(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return repo.create(params); }); | ||
} | ||
@@ -35,3 +36,3 @@ exports.create = create; | ||
function remove(repo, keys) { | ||
return IOEither_1.tryCatch(function () { return repo.delete(keys); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return repo.delete(keys); }); | ||
} | ||
@@ -43,4 +44,4 @@ exports.remove = remove; | ||
function query(repo, params) { | ||
return IOEither_1.tryCatch(function () { return repo.query(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return repo.query(params); }); | ||
} | ||
exports.query = query; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var IOEither_1 = require("fp-ts/lib/IOEither"); | ||
var utils_1 = require("./utils"); | ||
var portal = __non_webpack_require__("/lib/xp/portal"); | ||
function getContent() { | ||
return IOEither_1.tryCatch(function () { return portal.getContent(); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return portal.getContent(); }); | ||
} | ||
@@ -18,7 +17,7 @@ exports.getContent = getContent; | ||
function getSite() { | ||
return IOEither_1.tryCatch(function () { return portal.getSite(); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return portal.getSite(); }); | ||
} | ||
exports.getSite = getSite; | ||
function getSiteConfig() { | ||
return IOEither_1.tryCatch(function () { return portal.getSiteConfig(); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return portal.getSiteConfig(); }); | ||
} | ||
@@ -25,0 +24,0 @@ exports.getSiteConfig = getSiteConfig; |
@@ -8,36 +8,29 @@ "use strict"; | ||
function create(params) { | ||
return IOEither_1.tryCatch(function () { return repo.create(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return repo.create(params); }); | ||
} | ||
exports.create = create; | ||
function createBranch(params) { | ||
return IOEither_1.tryCatch(function () { return repo.createBranch(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return repo.createBranch(params); }); | ||
} | ||
exports.createBranch = createBranch; | ||
function get(id) { | ||
return pipeable_1.pipe(IOEither_1.tryCatch(function () { return repo.get(id); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }), IOEither_1.chain(utils_1.fromNullable({ errorKey: "NotFoundError" }))); | ||
return pipeable_1.pipe(utils_1.catchEnonicError(function () { return repo.get(id); }), IOEither_1.chain(utils_1.fromNullable({ errorKey: "NotFoundError" }))); | ||
} | ||
exports.get = get; | ||
function list() { | ||
return IOEither_1.tryCatch(function () { return repo.list(); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return repo.list(); }); | ||
} | ||
exports.list = list; | ||
function remove(id) { | ||
return IOEither_1.tryCatch(function () { return repo.delete(id); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return repo.delete(id); }); | ||
} | ||
exports.remove = remove; | ||
function deleteBranch(params) { | ||
// Figure out the shape here for any | ||
return pipeable_1.pipe(IOEither_1.tryCatch(function () { return repo.deleteBranch(params); }, function (e) { | ||
return { | ||
cause: String(e), | ||
errorKey: e.code === "branchNotFound" | ||
? "NotFoundError" | ||
: "InternalServerError" | ||
}; | ||
})); | ||
// TODO Figure out the shape here for "any" | ||
return utils_1.catchEnonicError(function () { return repo.deleteBranch(params); }); | ||
} | ||
exports.deleteBranch = deleteBranch; | ||
function refresh(params) { | ||
return IOEither_1.tryCatch(function () { return repo.refresh(params); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return repo.refresh(params); }); | ||
} | ||
exports.refresh = refresh; |
@@ -6,3 +6,5 @@ import { IOEither } from "fp-ts/lib/IOEither"; | ||
} | ||
export declare function renderUnsafe<A>(view: any, model?: A, options?: ThymeleafRenderOptions): string; | ||
export declare function render<A>(view: any, model?: A, options?: ThymeleafRenderOptions): IOEither<EnonicError, string>; | ||
export declare function getUnsafeRenderer<A>(view: any, options?: ThymeleafRenderOptions): (model: A) => string; | ||
export declare function getRenderer<A>(view: any, options?: ThymeleafRenderOptions): (model: A) => IOEither<EnonicError, string>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var IOEither_1 = require("fp-ts/lib/IOEither"); | ||
var utils_1 = require("./utils"); | ||
var thymeleaf = __non_webpack_require__("/lib/thymeleaf"); | ||
function renderUnsafe(view, model, options) { | ||
return thymeleaf.render(view, model, options); | ||
} | ||
exports.renderUnsafe = renderUnsafe; | ||
function render(view, model, options) { | ||
return IOEither_1.tryCatch(function () { return thymeleaf.render(view, model, options); }, function (e) { return ({ errorKey: "InternalServerError", cause: String(e) }); }); | ||
return utils_1.catchEnonicError(function () { return renderUnsafe(view, model, options); }); | ||
} | ||
exports.render = render; | ||
function getUnsafeRenderer(view, options) { | ||
return function (model) { return renderUnsafe(view, model, options); }; | ||
} | ||
exports.getUnsafeRenderer = getUnsafeRenderer; | ||
function getRenderer(view, options) { | ||
@@ -10,0 +18,0 @@ return function (model) { return render(view, model, options); }; |
declare const __non_webpack_require__: (path: string) => any; | ||
declare const Java: any; |
import { IOEither } from "fp-ts/lib/IOEither"; | ||
import { EnonicError, GeneralEnonicErrorKey } from "./common"; | ||
import { Lazy } from "fp-ts/lib/function"; | ||
export interface Throwable { | ||
getMessage(): string; | ||
getLocalizedMessage(): string; | ||
getCause(): Throwable; | ||
} | ||
export declare function fromNullable<E>(e: E): <A>(a: A | null | undefined) => IOEither<E, A>; | ||
export declare function isJavaThrowable(t: any): t is Throwable; | ||
export declare function getMessage(t: Throwable | unknown): string | undefined; | ||
export declare function getStackTraceAsString(t: Throwable | unknown): string | undefined; | ||
export declare function getRootCause(t: Throwable | unknown): Throwable | undefined; | ||
export declare function handleJavaThrowable(errorKey: GeneralEnonicErrorKey, t: Throwable | string | unknown): EnonicError; | ||
export declare function catchEnonicError<A>(f: Lazy<A>, errorKey?: GeneralEnonicErrorKey): IOEither<EnonicError, A>; |
@@ -5,2 +5,3 @@ "use strict"; | ||
var IOEither_1 = require("fp-ts/lib/IOEither"); | ||
var Throwables = Java.type('com.google.common.base.Throwables'); | ||
function fromNullable(e) { | ||
@@ -10,1 +11,47 @@ return function (a) { return IOEither_1.fromEither(EI.fromNullable(e)(a)); }; | ||
exports.fromNullable = fromNullable; | ||
function isJavaThrowable(t) { | ||
var throwable = t; | ||
return throwable && | ||
throwable.getMessage() !== undefined | ||
&& throwable.getCause() !== undefined; | ||
} | ||
exports.isJavaThrowable = isJavaThrowable; | ||
function getMessage(t) { | ||
return isJavaThrowable(t) | ||
? t.getMessage() | ||
: undefined; | ||
} | ||
exports.getMessage = getMessage; | ||
function getStackTraceAsString(t) { | ||
return isJavaThrowable(t) | ||
? Throwables.getStackTraceAsString(t) | ||
: undefined; | ||
} | ||
exports.getStackTraceAsString = getStackTraceAsString; | ||
function getRootCause(t) { | ||
return isJavaThrowable(t) | ||
? Throwables.getRootCause(t) | ||
: undefined; | ||
} | ||
exports.getRootCause = getRootCause; | ||
function handleJavaThrowable(errorKey, t) { | ||
if (isJavaThrowable(t)) { | ||
return { | ||
errorKey: errorKey, | ||
stackTrace: getStackTraceAsString(t), | ||
cause: getMessage(getRootCause(t)) | ||
}; | ||
} | ||
else { | ||
return { | ||
errorKey: errorKey, | ||
cause: String(t) | ||
}; | ||
} | ||
} | ||
exports.handleJavaThrowable = handleJavaThrowable; | ||
function catchEnonicError(f, errorKey) { | ||
if (errorKey === void 0) { errorKey = "InternalServerError"; } | ||
return IOEither_1.tryCatch(f, function (t) { return handleJavaThrowable(errorKey, t); }); | ||
} | ||
exports.catchEnonicError = catchEnonicError; |
{ | ||
"name": "enonic-fp", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Functional programming helpers for Enonic XP", | ||
@@ -10,4 +10,3 @@ "main": "lib/index.js", | ||
"build": "npm run clean && tsc", | ||
"lint:ts": "tslint --project tsconfig.json --config tslint.json --fix", | ||
"prettier:ts": "prettier --write 'src/**/*.ts'" | ||
"lint:ts": "eslint --fix 'src/**/*.ts'" | ||
}, | ||
@@ -29,12 +28,12 @@ "repository": { | ||
"dependencies": { | ||
"fp-ts": "^2.1.0" | ||
"fp-ts": "^2.1.1" | ||
}, | ||
"devDependencies": { | ||
"husky": "^3.0.5", | ||
"lint-staged": "^9.3.0", | ||
"prettier": "1.18.2", | ||
"@typescript-eslint/eslint-plugin": "^2.5.0", | ||
"@typescript-eslint/parser": "^2.5.0", | ||
"eslint": "^6.6.0", | ||
"husky": "^3.0.9", | ||
"lint-staged": "^9.4.2", | ||
"rimraf": "^3.0.0", | ||
"tslint": "^5.20.0", | ||
"tslint-config-prettier": "^1.18.0", | ||
"typescript": "^3.6.3" | ||
"typescript": "^3.6.4" | ||
}, | ||
@@ -48,4 +47,3 @@ "husky": { | ||
"*.ts": [ | ||
"tslint --project tsconfig.json --config tslint.json --fix", | ||
"prettier --write", | ||
"eslint --fix 'src/**/*.ts'", | ||
"git add" | ||
@@ -52,0 +50,0 @@ ] |
188
README.md
@@ -35,7 +35,9 @@ # Enonic FP | ||
```typescript | ||
import { Response, Request, Error } from "enonic-fp/lib/common"; | ||
import { Content, get as getContent } from 'enonic-fp/lib/content'; | ||
import { io } from "fp-ts/lib/IO"; | ||
import { pipe } from "fp-ts/lib/pipeable"; | ||
import { fold } from "fp-ts/lib/Either"; | ||
import { EnonicError, Request, Response } from "enonic-fp/lib/common"; | ||
import { Content, get as getContent } from 'enonic-fp/lib/content'; | ||
export function get(req: Request): Response { | ||
@@ -46,20 +48,22 @@ return pipe( | ||
}), | ||
fold<Error, Content<Article>, Response>( | ||
(err: Error) => ({ | ||
status: 500, EnonicError | ||
contentType: 'application/json', | ||
body: err | ||
}), | ||
(content: Content<Article>) => ({ | ||
status: 200, // 200 = Ok | ||
contentType: 'application/json', | ||
body: content | ||
}) | ||
fold<EnonicError, Content<Article>, Response>( | ||
(err: EnonicError) => | ||
io.of({ | ||
body: err, | ||
contentType: "application/json", | ||
status: 500 // 500 = Internal Server Error | ||
} as Response), | ||
(content: Content<Article>) => | ||
io.of({ | ||
body: content, | ||
contentType: "application/json", | ||
status: 200 // 200 = Ok | ||
}) | ||
) | ||
); | ||
)(); | ||
} | ||
interface Article { | ||
title: string, | ||
text: string | ||
title: string; | ||
text: string; | ||
} | ||
@@ -77,49 +81,52 @@ ``` | ||
```typescript | ||
import { Response, Request, Error } from "enonic-fp/lib/common"; | ||
import { remove, publish } from 'enonic-fp/lib/content'; | ||
import { run } from 'enonic-fp/lib/context'; | ||
import {IO, io} from "fp-ts/lib/IO"; | ||
import {chain, fold, IOEither} from "fp-ts/lib/IOEither"; | ||
import { pipe } from "fp-ts/lib/pipeable"; | ||
import { chain, fold } from "fp-ts/lib/Either"; | ||
import { EnonicError, Request, Response } from "enonic-fp/lib/common"; | ||
import {publish, PublishResponse, remove} from "enonic-fp/lib/context"; | ||
import { run } from "enonic-fp/lib/context"; | ||
function del(req: Request): Response { | ||
export function del(req: Request): Response { | ||
const key = req.params.key!!; | ||
return pipe( | ||
runInDraftContext(() => remove({ key })), | ||
runInDraftContext(remove({ key })), | ||
chain(() => publishToMaster(key)), | ||
fold<Error, any, Response>( | ||
(err: Error) => ({ | ||
status: errorKeyToStatus[err.errorKey], | ||
contentType: 'application/json', | ||
body: err | ||
}), | ||
() => ({ | ||
status: 204, // 204 = No content | ||
body: '' | ||
}) | ||
fold<EnonicError, any, Response>( | ||
(err: EnonicError) => | ||
io.of({ | ||
body: err, | ||
contentType: "application/json", | ||
status: errorKeyToStatus[err.errorKey] | ||
}), | ||
() => | ||
io.of({ | ||
body: "", | ||
status: 204 // 204 = No content | ||
}) | ||
) | ||
); | ||
)(); | ||
} | ||
export { del as delete }; // hack since delete is a keyword | ||
// --- HELPER FUNCTIONS --- | ||
function runInDraftContext<T>(f: () => T) { | ||
return run({ | ||
branch: 'draft' | ||
}, f); | ||
function runInDraftContext<A>(f: IO<A>): IO<A> { | ||
return run<A>( | ||
{ | ||
branch: "draft" | ||
} | ||
)(f); | ||
} | ||
function publishToMaster(key: string) { | ||
function publishToMaster(key: string): IOEither<EnonicError, PublishResponse> { | ||
return publish({ | ||
keys: [key], | ||
sourceBranch: 'draft', | ||
targetBranch: 'master', | ||
sourceBranch: "draft", | ||
targetBranch: "master" | ||
}); | ||
} | ||
const errorKeyToStatus : { [key: string]: number; } = { | ||
"InternalServerError": 500, | ||
"NotFoundError": 404, | ||
"PublishError": 500 | ||
const errorKeyToStatus: { [key: string]: number } = { | ||
InternalServerError: 500, | ||
NotFoundError: 404, | ||
PublishError: 500 | ||
}; | ||
@@ -143,16 +150,18 @@ ``` | ||
```typescript | ||
import { sequenceT } from "fp-ts/lib/Apply"; | ||
import { parseJSON } from "fp-ts/lib/Either"; | ||
import { io } from "fp-ts/lib/IO"; | ||
import { chain, fold, fromEither, ioEither, IOEither, map } from "fp-ts/lib/IOEither"; | ||
import { pipe } from "fp-ts/lib/pipeable"; | ||
import { chain, map, fold, either, Either, parseJSON } from "fp-ts/lib/Either"; | ||
import { sequenceT } from 'fp-ts/lib/Apply' | ||
import { Response, Request, Error } from "enonic-fp/lib/common"; | ||
import { Content, get as getContent, query, QueryResponse } from "enonic-fp/lib/content"; | ||
import { request} from "enonic-fp/lib/http"; | ||
import { EnonicError, Request, Response } from "enonic-fp/lib/common"; | ||
import { get as getContent, query, QueryResponse } from "enonic-fp/lib/content"; | ||
import { request } from "enonic-fp/lib/http"; | ||
export function get(req: Request): Response { | ||
const key = req.params.key!!; | ||
const articleKey = req.params.key!!; | ||
return pipe( | ||
sequenceT(either)( | ||
getArticle(key), | ||
getCommentsByArticleId(key), | ||
sequenceT(ioEither)( | ||
getContent<Article>({ key: articleKey }), | ||
getCommentsByArticleId(articleKey), | ||
getOpenPositionsOverHttp() | ||
@@ -163,56 +172,57 @@ ), | ||
...article, | ||
openPositions, | ||
comments: comments.hits, | ||
openPositions | ||
}; | ||
}), | ||
fold<Error, any, Response>( | ||
(err: Error) => ({ | ||
status: errorKeyToStatus[err.errorKey], | ||
contentType: 'application/json', | ||
body: err | ||
}), | ||
(res) => ({ | ||
status: 200, | ||
contentType: 'application/json', | ||
body: res | ||
}) | ||
fold<EnonicError, any, Response>( | ||
(err: EnonicError) => | ||
io.of({ | ||
body: err, | ||
contentType: "application/json", | ||
status: errorKeyToStatus[err.errorKey] | ||
}), | ||
(res) => | ||
io.of({ | ||
body: res, | ||
contentType: "application/json", | ||
status: 200 | ||
}) | ||
) | ||
) | ||
)(); | ||
} | ||
interface Article { | ||
title: string | ||
text: string | ||
title: string; | ||
text: string; | ||
} | ||
interface Comment { | ||
writtenBy: string, | ||
text: string | ||
writtenBy: string; | ||
text: string; | ||
} | ||
const errorKeyToStatus : { [key: string]: number; } = { | ||
"NotFoundError": 404, | ||
"InternalServerError": 500, | ||
"BadGatewayError": 502 | ||
const errorKeyToStatus: { [key: string]: number } = { | ||
BadGatewayError: 502, | ||
InternalServerError: 500, | ||
NotFoundError: 404 | ||
}; | ||
function getArticle(key: string) : Either<Error, Content<Article>> { | ||
return getContent({ key }); | ||
} | ||
function getCommentsByArticleId(articleId: string) : Either<Error, QueryResponse<Comment>> { | ||
function getCommentsByArticleId( | ||
articleId: string | ||
): IOEither<EnonicError, QueryResponse<Comment>> { | ||
return query({ | ||
query: `data.articleId = ${articleId}`, | ||
contentTypes: ['com.example:comment'] | ||
contentTypes: ["com.example:comment"], | ||
count: 100, | ||
query: `data.articleId = ${articleId}` | ||
}); | ||
} | ||
function createBadGatewayError(reason: any): Error { | ||
function createBadGatewayError(reason: any): EnonicError { | ||
return { | ||
errorKey: 'BadGatewayError', | ||
cause: String(reason) | ||
cause: String(reason), | ||
errorKey: "BadGatewayError" | ||
}; | ||
} | ||
function getOpenPositionsOverHttp() : Either<Error, any> { | ||
function getOpenPositionsOverHttp(): IOEither<EnonicError, any> { | ||
return pipe( | ||
@@ -222,4 +232,4 @@ request({ | ||
}), | ||
chain(res => parseJSON(res.body!, createBadGatewayError)) | ||
) | ||
chain(res => fromEither(parseJSON(res.body!, createBadGatewayError))) | ||
); | ||
} | ||
@@ -226,0 +236,0 @@ ``` |
52041
32
1316
237
Updatedfp-ts@^2.1.1