Comparing version 9.0.1 to 9.0.2
@@ -1,2 +0,2 @@ | ||
import { EndpointDescription, HistoricalFilter, RestResponse } from "./types"; | ||
import { EndpointDescription, HistoricalFilter, News, RestResponse, RestResponseError, RestResponseSuccess } from "./types"; | ||
export declare class Api { | ||
@@ -7,4 +7,8 @@ private apikey; | ||
changeApikey(apikey: string): void; | ||
search(filter: HistoricalFilter): Promise<RestResponse>; | ||
post<T, Z>(path: string, body: T): Promise<Z>; | ||
search(filter: HistoricalFilter, errorHandler?: (apiResponse: RestResponseError<News[]>) => void): Promise<RestResponseSuccess<News[]>>; | ||
getById(id: number, errorHandler?: (apiResponse: RestResponseError<News>) => void): Promise<RestResponseSuccess<News>>; | ||
get<T>(path: string, params?: any, errorHandler?: (apiResponse: RestResponseError<T>) => void): Promise<RestResponse<T>>; | ||
post<T, Z>(path: string, body: T, errorHandler?: (apiResponse: RestResponseError<Z>) => void): Promise<RestResponseSuccess<Z>>; | ||
handle<T>(res: Response, handleError: (apiResponse: RestResponseError<T>) => void): Promise<RestResponse<T>>; | ||
handleError<T>(apiResponse: RestResponseError<T>): Promise<void>; | ||
} |
@@ -26,11 +26,38 @@ "use strict"; | ||
} | ||
search(filter) { | ||
search(filter, errorHandler = this.handleError) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.post('/news', filter); | ||
return yield this.post('/news', filter, errorHandler); | ||
}); | ||
} | ||
post(path, body) { | ||
getById(id, errorHandler = this.handleError) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.get(`/news/${id}`, undefined, errorHandler); | ||
}); | ||
} | ||
get(path, params, errorHandler = this.handleError) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const endpoint = this.restEndpoint + path + (params ? "?" + new URLSearchParams(params) : ''); | ||
const res = yield (0, isomorphic_fetch_1.default)(endpoint, { | ||
method: "GET", | ||
headers: { | ||
'content-type': 'application/json', | ||
'x-api-key': this.apikey | ||
}, | ||
}); | ||
return this.handle(res, errorHandler); | ||
} | ||
catch (e) { | ||
if (((_b = (_a = e.cause) === null || _a === void 0 ? void 0 : _a.errors) === null || _b === void 0 ? void 0 : _b.length) > 0) { | ||
throw Error(e.cause.errors[0]); | ||
} | ||
throw e; | ||
} | ||
}); | ||
} | ||
post(path, body, errorHandler = this.handleError) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const res = yield (0, isomorphic_fetch_1.default)(this.restEndpoint + path, { | ||
@@ -44,7 +71,3 @@ method: "POST", | ||
}); | ||
const restResponse = yield res.json(); | ||
if (res.status < 200 || res.status > 299 || restResponse.error) { | ||
throw Error(`Status ${res.status}${restResponse.error ? ": " + restResponse.error.message : ""}`); | ||
} | ||
return restResponse; | ||
return this.handle(res, errorHandler); | ||
} | ||
@@ -59,4 +82,20 @@ catch (e) { | ||
} | ||
handle(res, handleError) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const restResponse = yield res.json(); | ||
if (res.status < 200 || res.status > 299) { | ||
yield handleError(restResponse); | ||
} | ||
return restResponse; | ||
}); | ||
} | ||
handleError(apiResponse) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
throw Error(`Status ${apiResponse.error.code}${apiResponse.error | ||
? ": " + apiResponse.error.message.charAt(0).toUpperCase() + apiResponse.error.message.slice(1) | ||
: ""}`); | ||
}); | ||
} | ||
} | ||
exports.Api = Api; | ||
//# sourceMappingURL=api.js.map |
@@ -237,3 +237,15 @@ "use strict"; | ||
})); | ||
it("get by id", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const api = new api_1.Api(context.config.apikey, enums_1.Endpoint.LOCALHOST); | ||
let actualNews = (yield api.getById(2)).data; | ||
(0, chai_1.expect)(actualNews.id).eq(2); | ||
})); | ||
it("get by id (404)", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const api = new api_1.Api(context.config.apikey, enums_1.Endpoint.LOCALHOST); | ||
yield api.getById(2000, function (response) { | ||
(0, chai_1.expect)(response.error.code).eq(404); | ||
(0, chai_1.expect)(response.error.message).eq("Resource not found"); | ||
}); | ||
})); | ||
}); | ||
//# sourceMappingURL=api.test.js.map |
export { Or, And, Text, Ciks, Tickers, Sources, CategoryCodes } from "./filters"; | ||
export { Api } from "./api"; | ||
export { WsApi } from "./wsApi"; | ||
export { News, Filter, TextOptions, WebsocketResponse, WebsocketErrorResponse, ConnectOptions, HistoricalFilter, RestResponse } from "./types"; | ||
export { News, Filter, TextOptions, WebsocketResponse, WebsocketErrorResponse, ConnectOptions, HistoricalFilter, RestResponse, RestResponseSuccess, RestResponseError } from "./types"; | ||
export { Source, WebsocketMethod, WebsocketResponseType, FilterAction, Field } from "./enums"; |
/// <reference types="ws" /> | ||
import { CloseEvent } from "isomorphic-ws"; | ||
import { Field, FilterAction, FilterType, WebsocketMethod, WebsocketResponseType } from "./enums"; | ||
export type RestResponse = { | ||
error: ApiResponseError; | ||
data: News[]; | ||
export type RestResponse<T> = RestResponseSuccess<T> | RestResponseError<T>; | ||
export type RestResponseSuccess<T> = { | ||
data: T; | ||
pagination?: { | ||
@@ -11,3 +11,10 @@ cursor: (string | number)[]; | ||
}; | ||
export interface ApiResponseError { | ||
export type RestResponseError<T> = { | ||
error: RestError; | ||
data: T; | ||
pagination?: { | ||
cursor: (string | number)[]; | ||
}; | ||
}; | ||
export interface RestError { | ||
code: number; | ||
@@ -14,0 +21,0 @@ message: string; |
@@ -79,3 +79,3 @@ "use strict"; | ||
.toString("utf-8")); | ||
const operations = seedData.flatMap(doc => [{ index: { _index: config.index } }, doc]); | ||
const operations = seedData.flatMap(doc => [{ index: { _index: config.index, _id: doc.id } }, doc]); | ||
const bulkResponse = yield client.bulk({ refresh: true, operations }); | ||
@@ -82,0 +82,0 @@ if (bulkResponse.errors) { |
{ | ||
"name": "newsware", | ||
"version": "9.0.1", | ||
"version": "9.0.2", | ||
"description": "Typescript client for interacting with the Newsware API", | ||
@@ -5,0 +5,0 @@ "main": "lib/src/index.js", |
@@ -289,2 +289,18 @@ import {Api} from "./api"; | ||
}) | ||
it("get by id", async () => { | ||
const api = new Api(context.config.apikey, Endpoint.LOCALHOST) | ||
let actualNews = (await api.getById(2)).data | ||
expect(actualNews.id).eq(2) | ||
}) | ||
it("get by id (404)", async () => { | ||
const api = new Api(context.config.apikey, Endpoint.LOCALHOST) | ||
await api.getById(2000, function (response) { | ||
expect(response.error.code).eq(404) | ||
expect(response.error.message).eq("Resource not found") | ||
}) | ||
}) | ||
}) |
@@ -1,2 +0,9 @@ | ||
import {EndpointDescription, HistoricalFilter, RestResponse} from "./types" | ||
import { | ||
EndpointDescription, | ||
HistoricalFilter, | ||
News, | ||
RestResponse, | ||
RestResponseError, | ||
RestResponseSuccess | ||
} from "./types" | ||
import {Endpoint} from "./enums" | ||
@@ -14,3 +21,3 @@ import fetch from "isomorphic-fetch" | ||
} | ||
changeApikey(apikey: string) { | ||
@@ -20,8 +27,40 @@ this.apikey = apikey | ||
async search(filter: HistoricalFilter): Promise<RestResponse> { | ||
return await this.post<HistoricalFilter, RestResponse>('/news', filter) | ||
async search(filter: HistoricalFilter, errorHandler: (apiResponse: RestResponseError<News[]>) => void = this.handleError): Promise<RestResponseSuccess<News[]>> { | ||
return await this.post<HistoricalFilter, News[]>('/news', filter, errorHandler) | ||
} | ||
async post<T, Z>(path: string, body: T): Promise<Z> { | ||
async getById(id: number, errorHandler: (apiResponse: RestResponseError<News>) => void = this.handleError): Promise<RestResponseSuccess<News>> { | ||
return await this.get<News>(`/news/${id}`, undefined, errorHandler) | ||
} | ||
async get<T>( | ||
path: string, | ||
params?: any, | ||
errorHandler: (apiResponse: RestResponseError<T>) => void = this.handleError | ||
): Promise<RestResponse<T>> { | ||
try { | ||
const endpoint = this.restEndpoint + path + (params ? "?" + new URLSearchParams(params) : '') | ||
const res = await fetch(endpoint, { | ||
method: "GET", | ||
headers: { | ||
'content-type': 'application/json', | ||
'x-api-key': this.apikey | ||
}, | ||
}) | ||
return this.handle(res, errorHandler) | ||
} catch (e: any) { | ||
if (e.cause?.errors?.length > 0) { | ||
throw Error(e.cause.errors[0]) | ||
} | ||
throw e | ||
} | ||
} | ||
async post<T, Z>( | ||
path: string, | ||
body: T, | ||
errorHandler: (apiResponse: RestResponseError<Z>) => void = this.handleError | ||
): Promise<RestResponseSuccess<Z>> { | ||
try { | ||
const res = await fetch(this.restEndpoint + path, { | ||
@@ -36,9 +75,3 @@ method: "POST", | ||
const restResponse = await res.json() as RestResponse | ||
if (res.status < 200 || res.status > 299 || restResponse.error) { | ||
throw Error(`Status ${res.status}${restResponse.error ? ": " + restResponse.error.message : ""}`) | ||
} | ||
return restResponse as Z | ||
return this.handle(res, errorHandler) | ||
} catch (e: any) { | ||
@@ -51,2 +84,20 @@ if (e.cause?.errors?.length > 0) { | ||
} | ||
async handle<T>(res: Response, handleError: (apiResponse: RestResponseError<T>) => void): Promise<RestResponse<T>> { | ||
const restResponse = await res.json() as RestResponseSuccess<T> | ||
if (res.status < 200 || res.status > 299) { | ||
await handleError(restResponse as RestResponseError<T>) | ||
} | ||
return restResponse | ||
} | ||
async handleError<T>(apiResponse: RestResponseError<T>) { | ||
throw Error(`Status ${apiResponse.error.code}${ | ||
apiResponse.error | ||
? ": " + apiResponse.error.message.charAt(0).toUpperCase() + apiResponse.error.message.slice(1) | ||
: "" | ||
}`) | ||
} | ||
} |
@@ -12,4 +12,6 @@ export {Or, And, Text, Ciks, Tickers, Sources, CategoryCodes} from "./filters"; | ||
HistoricalFilter, | ||
RestResponse | ||
RestResponse, | ||
RestResponseSuccess, | ||
RestResponseError | ||
} from "./types" | ||
export {Source, WebsocketMethod, WebsocketResponseType, FilterAction, Field} from "./enums" |
import {CloseEvent} from "isomorphic-ws" | ||
import {Field, FilterAction, FilterType, WebsocketMethod, WebsocketResponseType} from "./enums" | ||
export type RestResponse = { | ||
error: ApiResponseError | ||
data: News[] | ||
export type RestResponse<T> = RestResponseSuccess<T> | RestResponseError<T> | ||
export type RestResponseSuccess<T> = { | ||
data: T | ||
pagination?: { | ||
@@ -12,3 +13,11 @@ cursor: (string | number)[] | ||
export interface ApiResponseError { | ||
export type RestResponseError<T> = { | ||
error: RestError | ||
data: T | ||
pagination?: { | ||
cursor: (string | number)[] | ||
} | ||
} | ||
export interface RestError { | ||
code: number | ||
@@ -15,0 +24,0 @@ message: string |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
98854
1924