|
| 1 | +import { InvalidArgumentTypesError } from "./errors.ts"; |
1 | 2 | import { |
2 | 3 | AccountApiParameters, |
3 | 4 | AccountInformation, |
@@ -69,13 +70,44 @@ const SEARCH_ARCHIVE_PATH = `/searches`; |
69 | 70 | * } |
70 | 71 | * }); |
71 | 72 | */ |
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< |
73 | 105 | E extends EngineName = EngineName, |
74 | 106 | P extends AllowArbitraryParams<EngineParameters<E>> = EngineParameters<E>, |
75 | 107 | >( |
76 | 108 | parameters: P, |
77 | 109 | callback?: (json: BaseResponse<E>) => void, |
78 | | -) { |
| 110 | +): Promise<BaseResponse<E>> { |
79 | 111 | const key = validateApiKey(parameters.api_key, true); |
80 | 112 | const timeout = validateTimeout(parameters.timeout); |
81 | 113 | const response = await _internals.execute( |
|
0 commit comments