@serptech/api
Version:
Library for work with SERP API
44 lines (36 loc) • 1.06 kB
text/typescript
import { SerpWsApiV1, SerpWsApiV1Interface } from "./v1";
import { apiVersions, apiEndpoints } from "./constants";
import WebSocket from "isomorphic-ws";
interface SerpWsApiSettingsInterface {
token?: string;
version?: number | string;
endpoint?: string;
}
function createSerpWsApi(
settings: SerpWsApiSettingsInterface = {}
): SerpWsApiV1Interface | undefined {
const { token, version, endpoint } = settings;
if (!version) {
throw new Error(
`You did not specify Serp API version.
Available versions: ${apiVersions.join(", ")}`
);
}
const numVersion = Number(version);
if (!apiVersions.includes(numVersion)) {
throw new Error(
`You have specified a non-existent version of Serp API: ${version}.
Available versions: ${apiVersions.join(", ")}`
);
}
switch (numVersion) {
case 1:
return new SerpWsApiV1({
SocketClient: WebSocket,
token,
endpoint,
apiEndpoints,
});
}
}
export { createSerpWsApi };