Skip to content

Commit a6c20f7

Browse files
committed
Polymorphic getJson
1 parent 84550e9 commit a6c20f7

5 files changed

Lines changed: 49 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to
1111
### Added
1212

1313
- Expose `EngineName`, `EngineParameters` and `AllowArbitraryParams` types.
14+
- Expose `InvalidArgumentTypesError` error.
1415

1516
### Changed
1617

mod.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
export type { Config } from "./src/config.ts";
22
export { config } from "./src/config.ts";
33

4-
export { InvalidTimeoutError, MissingApiKeyError } from "./src/errors.ts";
4+
export {
5+
InvalidArgumentTypesError,
6+
InvalidTimeoutError,
7+
MissingApiKeyError,
8+
} from "./src/errors.ts";
59

610
export type {
711
AccountApiParameters,

src/errors.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
export class InvalidArgumentTypesError extends Error {
2+
constructor() {
3+
super("Provide the arguments with the correct type");
4+
Object.setPrototypeOf(this, InvalidArgumentTypesError.prototype);
5+
}
6+
}
7+
18
export class MissingApiKeyError extends Error {
29
constructor() {
310
super(

src/serpapi.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { InvalidArgumentTypesError } from "./errors.ts";
12
import {
23
AccountApiParameters,
34
AccountInformation,
@@ -69,13 +70,44 @@ const SEARCH_ARCHIVE_PATH = `/searches`;
6970
* }
7071
* });
7172
*/
72-
export async function getJson<
73+
export function getJson<
74+
E extends EngineName = EngineName,
75+
P1 extends AllowArbitraryParams<EngineParameters<E>> = EngineParameters<E>,
76+
P2 extends AllowArbitraryParams<EngineParameters<E, false>> =
77+
EngineParameters<E, false>,
78+
>(
79+
...args:
80+
| [parameters: P1, callback?: (json: BaseResponse<E>) => void]
81+
| [
82+
engine: string, // intentionally kept as a string to support arbitrary params
83+
parameters: P2,
84+
callback?: (json: BaseResponse<E>) => void,
85+
]
86+
): Promise<BaseResponse<E>> {
87+
if (
88+
typeof args[0] === "string" &&
89+
typeof args[1] === "object"
90+
) {
91+
const [engine, parameters, callback] = args;
92+
return _getJson({ ...parameters, engine: engine as E }, callback);
93+
} else if (
94+
typeof args[0] === "object" &&
95+
(typeof args[1] === "undefined" || typeof args[1] === "function")
96+
) {
97+
const [parameters, callback] = args;
98+
return _getJson(parameters, callback);
99+
} else {
100+
throw new InvalidArgumentTypesError();
101+
}
102+
}
103+
104+
async function _getJson<
73105
E extends EngineName = EngineName,
74106
P extends AllowArbitraryParams<EngineParameters<E>> = EngineParameters<E>,
75107
>(
76108
parameters: P,
77109
callback?: (json: BaseResponse<E>) => void,
78-
) {
110+
): Promise<BaseResponse<E>> {
79111
const key = validateApiKey(parameters.api_key, true);
80112
const timeout = validateTimeout(parameters.timeout);
81113
const response = await _internals.execute(

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ type AnyEngineName = string & {};
5353
export type EngineName = (keyof EngineMap) | AnyEngineName;
5454
export type EngineParameters<
5555
E extends EngineName = EngineName,
56+
W = true, // flag for whether `engine` is a required param
5657
> = {
5758
[K in E]:
58-
& { engine: K }
59+
& (W extends true ? { engine: K } : { engine?: K })
5960
& (K extends keyof EngineMap ? EngineMap[K]["parameters"] : BaseParameters);
6061
}[E];
6162

0 commit comments

Comments
 (0)