@botpress/messaging-client
Advanced tools
Comparing version 0.0.6 to 0.0.7
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.BaseClient = void 0; | ||
const axios_1 = __importDefault(require("axios")); | ||
const errors_1 = require("./errors"); | ||
const handleError = (err) => { | ||
var _a; | ||
if (axios_1.default.isAxiosError(err)) { | ||
switch ((_a = err.response) === null || _a === void 0 ? void 0 : _a.status) { | ||
case 400: | ||
throw new errors_1.BadRequestError(err.message); | ||
case 401: | ||
throw new errors_1.UnauthorizedError(err.message); | ||
case 403: | ||
throw new errors_1.ForbiddenError(err.message); | ||
case 404: | ||
return undefined; | ||
case 500: | ||
throw new errors_1.InternalServerError(err.message); | ||
default: | ||
throw err; | ||
} | ||
} | ||
throw err; | ||
}; | ||
class BaseClient { | ||
@@ -35,3 +11,3 @@ constructor(http) { | ||
}, (error) => { | ||
return handleError(error); | ||
return errors_1.handleError(error); | ||
}); | ||
@@ -38,0 +14,0 @@ } |
@@ -14,2 +14,3 @@ "use strict"; | ||
const base_1 = require("./base"); | ||
const errors_1 = require("./errors"); | ||
class ConversationClient extends base_1.BaseClient { | ||
@@ -23,3 +24,5 @@ create(userId) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.deserialize((yield this.http.get(`/conversations/${id}`)).data); | ||
return errors_1.handleNotFound(() => __awaiter(this, void 0, void 0, function* () { | ||
return this.deserialize((yield this.http.get(`/conversations/${id}`)).data); | ||
}), undefined); | ||
}); | ||
@@ -26,0 +29,0 @@ } |
@@ -10,4 +10,9 @@ export declare class BadRequestError extends Error { | ||
} | ||
export declare class NotFoundError extends Error { | ||
static statusCode: number; | ||
} | ||
export declare class InternalServerError extends Error { | ||
static statusCode: number; | ||
} | ||
export declare const handleError: (err: unknown) => never; | ||
export declare const handleNotFound: <U, T, F extends Function = () => U>(func: F, returnValue: T) => Promise<U | T>; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.InternalServerError = exports.ForbiddenError = exports.UnauthorizedError = exports.BadRequestError = void 0; | ||
exports.handleNotFound = exports.handleError = exports.InternalServerError = exports.NotFoundError = exports.ForbiddenError = exports.UnauthorizedError = exports.BadRequestError = void 0; | ||
const axios_1 = __importDefault(require("axios")); | ||
class BadRequestError extends Error { | ||
@@ -16,2 +29,6 @@ } | ||
ForbiddenError.statusCode = 403; | ||
class NotFoundError extends Error { | ||
} | ||
exports.NotFoundError = NotFoundError; | ||
NotFoundError.statusCode = 404; | ||
class InternalServerError extends Error { | ||
@@ -21,1 +38,36 @@ } | ||
InternalServerError.statusCode = 500; | ||
const handleError = (err) => { | ||
var _a; | ||
if (axios_1.default.isAxiosError(err)) { | ||
switch ((_a = err.response) === null || _a === void 0 ? void 0 : _a.status) { | ||
case 400: | ||
throw new BadRequestError(err.message); | ||
case 401: | ||
throw new UnauthorizedError(err.message); | ||
case 403: | ||
throw new ForbiddenError(err.message); | ||
case 404: | ||
throw new NotFoundError(err.message); | ||
case 500: | ||
throw new InternalServerError(err.message); | ||
default: | ||
throw err; | ||
} | ||
} | ||
throw err; | ||
}; | ||
exports.handleError = handleError; | ||
const handleNotFound = (func, returnValue) => __awaiter(void 0, void 0, void 0, function* () { | ||
try { | ||
return yield func(); | ||
} | ||
catch (err) { | ||
if (err instanceof NotFoundError) { | ||
return returnValue; | ||
} | ||
else { | ||
throw err; | ||
} | ||
} | ||
}); | ||
exports.handleNotFound = handleNotFound; |
@@ -5,3 +5,3 @@ import { Message } from '@botpress/messaging-base'; | ||
create(conversationId: string, authorId: string | undefined, payload: any): Promise<Message>; | ||
get(id: string): Promise<Message>; | ||
get(id: string): Promise<Message | undefined>; | ||
list(conversationId: string, limit: number): Promise<Message[]>; | ||
@@ -8,0 +8,0 @@ delete(filters: { |
@@ -14,2 +14,3 @@ "use strict"; | ||
const base_1 = require("./base"); | ||
const errors_1 = require("./errors"); | ||
class MessageClient extends base_1.BaseClient { | ||
@@ -23,3 +24,5 @@ create(conversationId, authorId, payload) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.deserialize((yield this.http.get(`/messages/${id}`)).data); | ||
return errors_1.handleNotFound(() => __awaiter(this, void 0, void 0, function* () { | ||
this.deserialize((yield this.http.get(`/messages/${id}`)).data); | ||
}), undefined); | ||
}); | ||
@@ -29,3 +32,5 @@ } | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return (yield this.http.get('/messages', { params: { conversationId, limit } })).data.map((x) => this.deserialize(x)); | ||
return errors_1.handleNotFound(() => __awaiter(this, void 0, void 0, function* () { | ||
return (yield this.http.get('/messages', { params: { conversationId, limit } })).data.map((x) => this.deserialize(x)); | ||
}), []); | ||
}); | ||
@@ -32,0 +37,0 @@ } |
@@ -14,2 +14,3 @@ "use strict"; | ||
const base_1 = require("./base"); | ||
const errors_1 = require("./errors"); | ||
class UserClient extends base_1.BaseClient { | ||
@@ -23,3 +24,5 @@ create() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return (yield this.http.get(`/users/${id}`)).data; | ||
return errors_1.handleNotFound(() => __awaiter(this, void 0, void 0, function* () { | ||
return (yield this.http.get(`/users/${id}`)).data; | ||
}), undefined); | ||
}); | ||
@@ -26,0 +29,0 @@ } |
{ | ||
"name": "@botpress/messaging-client", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
Sorry, the diff of this file is not supported yet
54087
439