@spinque/query-api
Advanced tools
Comparing version 0.14.0 to 0.15.0
@@ -1,3 +0,2 @@ | ||
import { ResultsResponse, StatisticsResponse } from '.'; | ||
import { ApiAuthenticationConfig, ApiConfig, ErrorResponse, Query, RequestOptions, RequestType, ResultItemTupleTypes } from './types'; | ||
import { ApiAuthenticationConfig, ApiConfig, ErrorResponse, Query, RequestOptions, RequestType, ResponseType, ResultItemTupleTypes } from './types'; | ||
/** | ||
@@ -52,4 +51,3 @@ * Send queries to the Spinque Query API using fetch. | ||
*/ | ||
fetch<T = ResultsResponse | ResultsResponse<ResultItemTupleTypes[]>>(queries: Query | Query[], options?: RequestOptions, requestType?: RequestType): Promise<ResultsResponse<T> | ErrorResponse>; | ||
fetch(queries: Query | Query[], options: RequestOptions, requestType: 'statistics'): Promise<StatisticsResponse | ErrorResponse>; | ||
fetch<T extends ResultItemTupleTypes[] = ResultItemTupleTypes[], R extends RequestType = 'results'>(queries: Query | Query[], options?: RequestOptions, requestType?: R): Promise<ResponseType<R, T> | ErrorResponse>; | ||
/** | ||
@@ -56,0 +54,0 @@ * Handle the response of a fetch to Spinque Query API. |
@@ -40,3 +40,2 @@ "use strict"; | ||
exports.Api = void 0; | ||
var cross_fetch_1 = require("cross-fetch"); | ||
var authentication_1 = require("./authentication"); | ||
@@ -133,4 +132,6 @@ var types_1 = require("./types"); | ||
}); | ||
/** | ||
* Fetch a Query (or array of Queries). Takes optional RequestOptions and RequestType into account. | ||
*/ | ||
Api.prototype.fetch = function (queries, options, requestType) { | ||
if (requestType === void 0) { requestType = 'results'; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -155,7 +156,7 @@ var url, requestInit, token; | ||
token = _a.sent(); | ||
requestInit = { headers: new cross_fetch_1.Headers({ Authorization: "Bearer ".concat(token) }) }; | ||
requestInit = { headers: new Headers({ Authorization: "Bearer ".concat(token) }) }; | ||
_a.label = 2; | ||
case 2: | ||
// Make the request | ||
return [2 /*return*/, (0, cross_fetch_1.default)(url, requestInit).then(function (res) { return _this.handleResponse(res); })]; | ||
return [2 /*return*/, fetch(url, requestInit).then(function (res) { return _this.handleResponse(res); })]; | ||
} | ||
@@ -162,0 +163,0 @@ }); |
@@ -74,3 +74,2 @@ "use strict"; | ||
var browser_or_node_1 = require("browser-or-node"); | ||
var cross_fetch_1 = require("cross-fetch"); | ||
/** | ||
@@ -119,5 +118,5 @@ * An Authenticator class for the OAuth 2.0 Client Credentials grant. | ||
}; | ||
return [4 /*yield*/, (0, cross_fetch_1.default)((0, utils_1.join)(authServer, 'oauth', 'token'), { | ||
return [4 /*yield*/, fetch((0, utils_1.join)(authServer, 'oauth', 'token'), { | ||
method: 'POST', | ||
headers: new cross_fetch_1.Headers({ 'Content-Type': 'application/x-www-form-urlencoded' }), | ||
headers: new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' }), | ||
// URL Encode the body | ||
@@ -124,0 +123,0 @@ body: Object.entries(body) |
@@ -83,3 +83,2 @@ "use strict"; | ||
var browser_or_node_1 = require("browser-or-node"); | ||
var cross_fetch_1 = require("cross-fetch"); | ||
/** | ||
@@ -197,5 +196,5 @@ * An Authenticator class for the OAuth 2.0 Authorization Code with PKCE grant. | ||
}; | ||
return [4 /*yield*/, (0, cross_fetch_1.default)((0, utils_1.join)(authServer, 'oauth', 'token'), { | ||
return [4 /*yield*/, fetch((0, utils_1.join)(authServer, 'oauth', 'token'), { | ||
method: 'POST', | ||
headers: new cross_fetch_1.Headers({ 'Content-Type': 'application/x-www-form-urlencoded' }), | ||
headers: new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' }), | ||
body: Object.entries(body) | ||
@@ -202,0 +201,0 @@ .map(function (_a) { |
@@ -100,3 +100,3 @@ /** | ||
*/ | ||
export declare type RequestType = 'results' | 'statistics'; | ||
export declare type RequestType = 'results' | 'statistics' | 'count' | 'results,count'; | ||
export interface RequestOptions { | ||
@@ -110,5 +110,14 @@ count?: number; | ||
/** | ||
* Map from RequestType to a Response type | ||
*/ | ||
declare type ResponseMap<T = ResultItemTupleTypes[]> = { | ||
results: ResultsResponse<T>; | ||
statistics: StatisticsResponse; | ||
count: CountResponse; | ||
'results,count': ResultsAndCountResponse<T>; | ||
}; | ||
/** | ||
* ResponseType based on RequestType | ||
*/ | ||
export declare type ResponseType<T extends RequestType, U = ResultsResponse | ResultsResponse<ResultItemTupleTypes[]>> = T extends 'results' ? ResultsResponse<U> : StatisticsResponse; | ||
export declare type ResponseType<U extends RequestType, T = ResultItemTupleTypes[]> = U extends keyof ResponseMap ? ResponseMap<T>[U] : never; | ||
/** | ||
@@ -172,2 +181,9 @@ * Data types known by Spinque. | ||
/** | ||
* Response to a Query that contains the count | ||
*/ | ||
export interface CountResponse { | ||
total: number; | ||
} | ||
export declare type ResultsAndCountResponse<T = ResultItemTupleTypes[]> = [ResultsResponse<T>, StatisticsResponse]; | ||
/** | ||
* Generic error response class. Is implemented by more specific error type classes. | ||
@@ -207,1 +223,2 @@ */ | ||
} | ||
export {}; |
{ | ||
"name": "@spinque/query-api", | ||
"version": "0.14.0", | ||
"version": "0.15.0", | ||
"description": "", | ||
@@ -9,3 +9,3 @@ "main": "dist/index.js", | ||
"build": "tsc", | ||
"demo": "npx ts-node -r tsconfig-paths/register ./demo/basic/clustered-search.ts", | ||
"demo": "npx ts-node -r tsconfig-paths/register ./demo/authentication/server/index.ts", | ||
"docs": "typedoc src/index.ts", | ||
@@ -45,9 +45,8 @@ "check-format": "prettier --check \"src/**/*.ts\"", | ||
"tsconfig-paths": "^4.2.0", | ||
"typescript": "^4.6.2", | ||
"typedoc": "0.24.8" | ||
"typedoc": "0.24.8", | ||
"typescript": "^4.6.2" | ||
}, | ||
"dependencies": { | ||
"browser-or-node": "^2.1.1", | ||
"cross-fetch": "^3.1.6" | ||
"browser-or-node": "^2.1.1" | ||
} | ||
} |
@@ -35,2 +35,4 @@ # @spinque/query-api | ||
Note: when using this library with NodeJS, version >= 18 is expected for `fetch` support. | ||
## Documentation | ||
@@ -82,3 +84,3 @@ | ||
// Fetch the 10 first results of the query | ||
const response = await api.fetch(query, { count: 10 }); | ||
const results = await api.fetch(query, { count: 10 }); | ||
} catch (error: any) { | ||
@@ -89,2 +91,8 @@ console.error(error); | ||
You can also fetch other type of responses: | ||
```typescript | ||
const statistics = await api.fetch(query, { count: 10 }, 'statistics'); | ||
const resultsWithCount = await api.fetch(query, { count: 10 }, 'results,count'); | ||
``` | ||
### Fetching using custom HTTP-library | ||
@@ -91,0 +99,0 @@ |
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
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
Network access
Supply chain riskThis module accesses the network.
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
161291
1
2382
525
3
- Removedcross-fetch@^3.1.6
- Removedcross-fetch@3.1.8(transitive)
- Removednode-fetch@2.7.0(transitive)
- Removedtr46@0.0.3(transitive)
- Removedwebidl-conversions@3.0.1(transitive)
- Removedwhatwg-url@5.0.0(transitive)