Comparing version 4.0.0 to 4.1.0
{ | ||
"name": "newsware", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Typescript client for interacting with the Newsware API", | ||
"main": "lib/index.js", | ||
"main": "lib/src/index.js", | ||
"files": [ | ||
@@ -10,2 +10,3 @@ "lib", | ||
], | ||
"types": "./lib/src/index.d.ts", | ||
"scripts": { | ||
@@ -15,3 +16,4 @@ "prepare": "npm run build", | ||
"bundle": "browserify lib/index.js -o lib/bundle.js", | ||
"publish-package": "npm run build && npm publish" | ||
"publish-package": "npm run build && npm publish", | ||
"test": "mocha -r ts-node/register -r './test/setup.ts' './src/**/*.test.ts'" | ||
}, | ||
@@ -38,3 +40,11 @@ "repository": { | ||
"devDependencies": { | ||
"@elastic/elasticsearch": "^8.10.0", | ||
"@types/chai": "^4.3.6", | ||
"@types/js-yaml": "^4.0.6", | ||
"@types/mocha": "^10.0.1", | ||
"@types/ws": "^8.5.5", | ||
"chai": "^4.3.8", | ||
"js-yaml": "^4.1.0", | ||
"mocha": "^10.2.0", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.1.3" | ||
@@ -41,0 +51,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
import {ApiHost, Response, SubscribeOptions} from "./types"; | ||
import {Endpoint, HistoricalFilter, News, ApiResponse, SubscribeOptions, EndpointDescription} from "./types"; | ||
import WebSocket, {MessageEvent, ErrorEvent, CloseEvent} from "isomorphic-ws" | ||
@@ -7,7 +7,11 @@ | ||
private subscribed = false | ||
private websocketEndpoint: string | ||
private restEndpoint: string | ||
constructor( | ||
private apikey: string, | ||
private host: string = ApiHost.Production | ||
endpoint: EndpointDescription = Endpoint.Production | ||
) { | ||
this.websocketEndpoint = endpoint.websocketProtocol + "://" + endpoint.host | ||
this.restEndpoint = endpoint.restProtocol + "://" + endpoint.host | ||
} | ||
@@ -23,6 +27,6 @@ | ||
}) | ||
this.socket = new WebSocket(`${this.host}/v1/ws/news?${urlParams.toString()}`) | ||
this.socket = new WebSocket(`${this.websocketEndpoint}/v1/ws/news?${urlParams.toString()}`) | ||
this.socket.onmessage = (event: MessageEvent) => { | ||
const response = JSON.parse(event.data.toString()) as Response | ||
const response = JSON.parse(event.data.toString()) as ApiResponse | ||
if (response.error && this.socket?.onerror) { | ||
@@ -66,2 +70,32 @@ this.socket.onerror({ | ||
} | ||
async search(filter: HistoricalFilter): Promise<News[]> { | ||
return await this.post<HistoricalFilter, News[]>('/v1/api/news', filter) | ||
} | ||
async post<T, Z>(path: string, body: T): Promise<Z> { | ||
try { | ||
const res = await fetch(this.restEndpoint + path, { | ||
method: "POST", | ||
body: JSON.stringify(body), | ||
headers: { | ||
'content-type': 'application/json', | ||
'x-api-key': this.apikey | ||
}, | ||
}) | ||
const apiResponse = await res.json() as ApiResponse | ||
if (res.status < 200 || res.status > 299 || apiResponse.error) { | ||
throw Error(`Status ${res.status}${apiResponse.error ? ": " + apiResponse.error.message : ""}`) | ||
} | ||
return apiResponse.data as Z | ||
} catch (e: any) { | ||
if (e.cause?.errors?.length > 0) { | ||
throw Error(e.cause.errors[0]) | ||
} | ||
throw e | ||
} | ||
} | ||
} |
export {or, and, text} from "./queries"; | ||
export {Api} from "./api" | ||
export {News, Filter, TextOptions, ApiHost, Source} from "./types" | ||
export {News, Filter, TextOptions, Endpoint, EndpointDescription, Source} from "./types" |
@@ -10,8 +10,8 @@ import {CloseEvent, ErrorEvent} from "isomorphic-ws"; | ||
export interface Response { | ||
error: ResponseError; | ||
export interface ApiResponse { | ||
error: ApiResponseError; | ||
data: News[]; | ||
} | ||
export interface ResponseError { | ||
export interface ApiResponseError { | ||
code: number; | ||
@@ -54,7 +54,21 @@ message: string; | ||
export enum ApiHost { | ||
Localhost = "ws://localhost:8080", | ||
Production = "wss://newswareapi.encypherstudio.com" | ||
export interface EndpointDescription { | ||
host: string | ||
websocketProtocol: string | ||
restProtocol: string | ||
} | ||
export const Endpoint: {[key: string]: EndpointDescription} = { | ||
Localhost: { | ||
host: "localhost:8080", | ||
websocketProtocol: "ws", | ||
restProtocol: "http" | ||
}, | ||
Production: { | ||
host: "newswareapi.encypherstudio.com", | ||
websocketProtocol: "wss", | ||
restProtocol: "https" | ||
}, | ||
} | ||
export enum Source { | ||
@@ -76,2 +90,13 @@ DowJones = "DJ", | ||
automaticReconnect?: boolean | ||
} | ||
export interface HistoricalFilter extends Filter { | ||
pagination?: Pagination | ||
publishedAfter?: number | ||
publishedBefore?: number | ||
} | ||
export interface Pagination { | ||
limit?: number | ||
page?: number | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43613
878
10
25
1
2