blocket.js 1.0.6 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/index.js +4 -3
- package/dist/client/index.js.map +1 -1
- package/dist/client/request.js +1 -1
- package/dist/client/request.js.map +1 -1
- package/dist/client/token.js +1 -1
- package/dist/client/token.js.map +1 -1
- package/dist/config/index.d.ts +53 -3
- package/dist/config/index.js +25 -5
- package/dist/config/index.js.map +1 -1
- package/lib/client/index.ts +5 -4
- package/lib/client/request.ts +2 -2
- package/lib/client/token.ts +2 -2
- package/lib/config/index.ts +69 -4
- package/package.json +1 -1
package/dist/client/index.js
CHANGED
|
@@ -36,8 +36,9 @@ function remapQueryParams(params) {
|
|
|
36
36
|
*/
|
|
37
37
|
function find(query, fetchOptions) {
|
|
38
38
|
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
-
const config = (0, config_1.
|
|
40
|
-
const
|
|
39
|
+
const config = (0, config_1.getBaseConfig)();
|
|
40
|
+
const queryConfig = (0, config_1.createQueryConfig)(query);
|
|
41
|
+
const response = yield (0, request_1.apiRequest)(config.apiBaseUrl, Object.assign({ query: remapQueryParams(queryConfig) }, fetchOptions));
|
|
41
42
|
if (!response || !response.data || !Array.isArray(response.data)) {
|
|
42
43
|
throw new Error(`Unexpected Blocket API response structure, expected array of ads, got: ${typeof (response === null || response === void 0 ? void 0 : response.data)}`);
|
|
43
44
|
}
|
|
@@ -52,7 +53,7 @@ function find(query, fetchOptions) {
|
|
|
52
53
|
*/
|
|
53
54
|
function getAd(adId, fetchOptions) {
|
|
54
55
|
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
-
const config = (0, config_1.
|
|
56
|
+
const config = (0, config_1.getBaseConfig)();
|
|
56
57
|
const url = `${config.apiBaseUrl}/ad/${adId}`;
|
|
57
58
|
return yield (0, request_1.apiRequest)(url, fetchOptions);
|
|
58
59
|
});
|
package/dist/client/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/client/index.ts"],"names":[],"mappings":";;;;;;;;;;;AA6BA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/client/index.ts"],"names":[],"mappings":";;;;;;;;;;;AA6BA,oBAkBC;AA/CD,uCAAuC;AACvC,sCAA6D;AAK7D;;;;GAIG;AACH,SAAS,gBAAgB,CAAC,MAA0B;IAClD,OAAO;QACL,CAAC,EAAE,MAAM,CAAC,KAAK;QACf,GAAG,EAAE,MAAM,CAAC,KAAK;QACjB,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,EAAE,EAAE,MAAM,CAAC,WAAW;QACtB,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,EAAE,EAAE,MAAM,CAAC,EAAE;QACb,OAAO,EAAE,MAAM,CAAC,OAAO;KACxB,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAsB,IAAI,CACxB,KAAyB,EACzB,YAAwC;;QAExC,MAAM,MAAM,GAAG,IAAA,sBAAa,GAAE,CAAC;QAC/B,MAAM,WAAW,GAAG,IAAA,0BAAiB,EAAC,KAAK,CAAC,CAAC;QAC7C,MAAM,QAAQ,GAAG,MAAM,IAAA,oBAAU,EAAkB,MAAM,CAAC,UAAU,kBAClE,KAAK,EAAE,gBAAgB,CAAC,WAAW,CAAC,IACjC,YAAY,EACf,CAAC;QAEH,IAAI,CAAC,QAAQ,IAAI,CAAC,QAAQ,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YACjE,MAAM,IAAI,KAAK,CACb,0EAA0E,OAAO,CAAA,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,IAAI,CAAA,EAAE,CAClG,CAAC;QACJ,CAAC;QAED,OAAO,QAAQ,CAAC,IAAI,CAAC;IACvB,CAAC;CAAA;AAED;;;;;GAKG;AACH,SAAe,KAAK,CAClB,IAAY,EACZ,YAAwC;;QAExC,MAAM,MAAM,GAAG,IAAA,sBAAa,GAAE,CAAC;QAC/B,MAAM,GAAG,GAAG,GAAG,MAAM,CAAC,UAAU,OAAO,IAAI,EAAE,CAAC;QAE9C,OAAO,MAAM,IAAA,oBAAU,EAAY,GAAG,EAAE,YAAY,CAAC,CAAC;IACxD,CAAC;CAAA"}
|
package/dist/client/request.js
CHANGED
|
@@ -23,7 +23,7 @@ const config_1 = require("../config");
|
|
|
23
23
|
function apiRequest(url_1) {
|
|
24
24
|
return __awaiter(this, arguments, void 0, function* (url, options = {}, retryCount = 0) {
|
|
25
25
|
var _a;
|
|
26
|
-
const config = (0, config_1.
|
|
26
|
+
const config = (0, config_1.getBaseConfig)();
|
|
27
27
|
const token = yield (0, token_1.fetchToken)();
|
|
28
28
|
try {
|
|
29
29
|
return yield (0, ofetch_1.ofetch)(url, Object.assign(Object.assign({}, options), { headers: Object.assign(Object.assign({}, options.headers), { Authorization: `Bearer ${token}` }) }));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../lib/client/request.ts"],"names":[],"mappings":";;;;;;;;;;;AAYA,gCA8BC;AA1CD,mCAAmD;AAEnD,mCAAqD;AACrD,
|
|
1
|
+
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../lib/client/request.ts"],"names":[],"mappings":";;;;;;;;;;;AAYA,gCA8BC;AA1CD,mCAAmD;AAEnD,mCAAqD;AACrD,sCAAkD;AAElD;;;;;;GAMG;AACH,SAAsB,UAAU;yDAC9B,GAAW,EACX,UAAqC,EAAE,EACvC,aAAqB,CAAC;;QAEtB,MAAM,MAAM,GAAG,IAAA,sBAAa,GAAE,CAAC;QAC/B,MAAM,KAAK,GAAG,MAAM,IAAA,kBAAU,GAAE,CAAC;QAEjC,IAAI,CAAC;YACH,OAAO,MAAM,IAAA,eAAM,EAAI,GAAG,kCACrB,OAAO,KACV,OAAO,kCACF,OAAO,CAAC,OAAO,KAClB,aAAa,EAAE,UAAU,KAAK,EAAE,OAElC,CAAC;QACL,CAAC;QAAC,OAAO,KAAU,EAAE,CAAC;YACpB,IAAI,CAAA,MAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,IAAI,0CAAE,WAAW,MAAK,GAAG,IAAI,UAAU,GAAG,MAAM,CAAC,aAAa,EAAE,CAAC;gBAC1E,IAAA,eAAM,EACJ,MAAM,EACN,oCAAoC,UAAU,GAAG,CAAC,IAChD,MAAM,CAAC,aACT,IAAI,CACL,CAAC;gBACF,MAAM,QAAQ,GAAG,MAAM,IAAA,kBAAU,EAAC,IAAI,CAAC,CAAC;gBACxC,IAAA,sBAAc,EAAC,QAAQ,CAAC,CAAC;gBACzB,OAAO,UAAU,CAAI,GAAG,EAAE,OAAO,EAAE,UAAU,GAAG,CAAC,CAAC,CAAC;YACrD,CAAC;YACD,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CAAA"}
|
package/dist/client/token.js
CHANGED
|
@@ -36,7 +36,7 @@ const fetchToken = (...args_1) => __awaiter(void 0, [...args_1], void 0, functio
|
|
|
36
36
|
if (!forceRefresh && cachedToken)
|
|
37
37
|
return cachedToken;
|
|
38
38
|
(0, config_1.logger)('debug', 'Fetching new Blocket API token.');
|
|
39
|
-
const config = (0, config_1.
|
|
39
|
+
const config = (0, config_1.getBaseConfig)();
|
|
40
40
|
const tokenData = yield (0, ofetch_1.ofetch)(config.tokenEndpoint);
|
|
41
41
|
if (!tokenData || !tokenData.bearerToken) {
|
|
42
42
|
throw new Error('Failed to retrieve Blocket API token.');
|
package/dist/client/token.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.js","sourceRoot":"","sources":["../../lib/client/token.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mCAAgC;AAEhC,
|
|
1
|
+
{"version":3,"file":"token.js","sourceRoot":"","sources":["../../lib/client/token.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mCAAgC;AAEhC,sCAAkD;AAIlD,IAAI,WAAW,GAAkB,IAAI,CAAC;AAEtC;;;GAGG;AACI,MAAM,cAAc,GAAG,GAAkB,EAAE,CAAC,WAAW,CAAC;AAAlD,QAAA,cAAc,kBAAoC;AAE/D;;;GAGG;AACI,MAAM,cAAc,GAAG,CAAC,KAAa,EAAQ,EAAE;IACpD,WAAW,GAAG,KAAK,CAAC;AACtB,CAAC,CAAC;AAFW,QAAA,cAAc,kBAEzB;AAEF;;;;GAIG;AACI,MAAM,UAAU,GAAG,YAEP,EAAE,mDADnB,eAAwB,KAAK;IAE7B,IAAI,CAAC,YAAY,IAAI,WAAW;QAAE,OAAO,WAAW,CAAC;IAErD,IAAA,eAAM,EAAC,OAAO,EAAE,iCAAiC,CAAC,CAAC;IAEnD,MAAM,MAAM,GAAG,IAAA,sBAAa,GAAE,CAAC;IAE/B,MAAM,SAAS,GAAG,MAAM,IAAA,eAAM,EAAqB,MAAM,CAAC,aAAa,CAAC,CAAC;IACzE,IAAI,CAAC,SAAS,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;IAC3D,CAAC;IAED,WAAW,GAAG,SAAS,CAAC,WAAW,CAAC;IACpC,OAAO,WAAW,CAAC;AACrB,CAAC,CAAA,CAAC;AAhBW,QAAA,UAAU,cAgBrB"}
|
package/dist/config/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Configuration interface.
|
|
2
|
+
* Blocket.js Global Configuration interface.
|
|
3
3
|
*/
|
|
4
4
|
export interface BlocketConfig {
|
|
5
5
|
/**
|
|
@@ -25,7 +25,7 @@ export interface BlocketConfig {
|
|
|
25
25
|
retryAttempts: number;
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
|
-
* Default configuration.
|
|
28
|
+
* Default global configuration.
|
|
29
29
|
*/
|
|
30
30
|
export declare const defaultConfig: BlocketConfig;
|
|
31
31
|
/**
|
|
@@ -37,10 +37,60 @@ export declare const configure: (config: Partial<BlocketConfig>) => void;
|
|
|
37
37
|
* Get the current configuration.
|
|
38
38
|
* @returns The current Blocket.js configuration.
|
|
39
39
|
*/
|
|
40
|
-
export declare const
|
|
40
|
+
export declare const getBaseConfig: () => BlocketConfig;
|
|
41
41
|
/**
|
|
42
42
|
* Simple logger function based on log level.
|
|
43
43
|
* @param level Log level of the message.
|
|
44
44
|
* @param message Message to log.
|
|
45
45
|
*/
|
|
46
46
|
export declare const logger: (level: "error" | "info" | "debug", message: string) => void;
|
|
47
|
+
/**
|
|
48
|
+
* Blocket Query Configuration interface.
|
|
49
|
+
* The only required property is the query string.
|
|
50
|
+
*/
|
|
51
|
+
export interface BlocketQueryConfig {
|
|
52
|
+
/**
|
|
53
|
+
* The search query string.
|
|
54
|
+
*/
|
|
55
|
+
query: string;
|
|
56
|
+
/**
|
|
57
|
+
* The maximum number of results to return.
|
|
58
|
+
* @default 100
|
|
59
|
+
*/
|
|
60
|
+
limit?: number;
|
|
61
|
+
/**
|
|
62
|
+
* The sorting order of the results.
|
|
63
|
+
* @default 'rel'
|
|
64
|
+
*/
|
|
65
|
+
sort?: 'rel';
|
|
66
|
+
/**
|
|
67
|
+
* The type of listing to search for. 's' for selling, 'b' for buying.
|
|
68
|
+
* @default 's'
|
|
69
|
+
*/
|
|
70
|
+
listingType?: 's' | 'b';
|
|
71
|
+
/**
|
|
72
|
+
* The status of the ad.
|
|
73
|
+
* @default 'active'
|
|
74
|
+
*/
|
|
75
|
+
status?: 'active' | 'inactive' | string;
|
|
76
|
+
/**
|
|
77
|
+
* The maximum distance in kilometers from the search location.
|
|
78
|
+
* @default 3
|
|
79
|
+
*/
|
|
80
|
+
gl?: number;
|
|
81
|
+
/**
|
|
82
|
+
* Additional filters or fields to include in the response.
|
|
83
|
+
* @default 'extend_with_shipping'
|
|
84
|
+
*/
|
|
85
|
+
include?: string;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Default query configuration (excluding the required query string).
|
|
89
|
+
*/
|
|
90
|
+
export declare const defaultQueryConfig: Omit<BlocketQueryConfig, 'query'>;
|
|
91
|
+
/**
|
|
92
|
+
* Merges the provided query configuration with default values.
|
|
93
|
+
* @param queryConfig Partial query configuration that must include the query string.
|
|
94
|
+
* @returns A complete BlocketQueryConfig object.
|
|
95
|
+
*/
|
|
96
|
+
export declare const createQueryConfig: (queryConfig: Pick<BlocketQueryConfig, "query"> & Partial<Omit<BlocketQueryConfig, "query">>) => BlocketQueryConfig;
|
package/dist/config/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.logger = exports.
|
|
3
|
+
exports.createQueryConfig = exports.defaultQueryConfig = exports.logger = exports.getBaseConfig = exports.configure = exports.defaultConfig = void 0;
|
|
4
4
|
/**
|
|
5
|
-
* Default configuration.
|
|
5
|
+
* Default global configuration.
|
|
6
6
|
*/
|
|
7
7
|
exports.defaultConfig = {
|
|
8
8
|
apiBaseUrl: 'https://api.blocket.se/search_bff/v2/content',
|
|
@@ -23,8 +23,8 @@ exports.configure = configure;
|
|
|
23
23
|
* Get the current configuration.
|
|
24
24
|
* @returns The current Blocket.js configuration.
|
|
25
25
|
*/
|
|
26
|
-
const
|
|
27
|
-
exports.
|
|
26
|
+
const getBaseConfig = () => currentConfig;
|
|
27
|
+
exports.getBaseConfig = getBaseConfig;
|
|
28
28
|
/**
|
|
29
29
|
* Simple logger function based on log level.
|
|
30
30
|
* @param level Log level of the message.
|
|
@@ -32,9 +32,29 @@ exports.getConfig = getConfig;
|
|
|
32
32
|
*/
|
|
33
33
|
const logger = (level, message) => {
|
|
34
34
|
const levels = { none: 0, error: 1, info: 2, debug: 3 };
|
|
35
|
-
if (levels[(0, exports.
|
|
35
|
+
if (levels[(0, exports.getBaseConfig)().logLevel] >= levels[level]) {
|
|
36
36
|
console[level === 'error' ? 'error' : 'log'](message);
|
|
37
37
|
}
|
|
38
38
|
};
|
|
39
39
|
exports.logger = logger;
|
|
40
|
+
/**
|
|
41
|
+
* Default query configuration (excluding the required query string).
|
|
42
|
+
*/
|
|
43
|
+
exports.defaultQueryConfig = {
|
|
44
|
+
limit: 100,
|
|
45
|
+
sort: 'rel',
|
|
46
|
+
listingType: 's',
|
|
47
|
+
status: 'active',
|
|
48
|
+
gl: 3,
|
|
49
|
+
include: 'extend_with_shipping',
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Merges the provided query configuration with default values.
|
|
53
|
+
* @param queryConfig Partial query configuration that must include the query string.
|
|
54
|
+
* @returns A complete BlocketQueryConfig object.
|
|
55
|
+
*/
|
|
56
|
+
const createQueryConfig = (queryConfig) => {
|
|
57
|
+
return Object.assign(Object.assign({}, exports.defaultQueryConfig), queryConfig);
|
|
58
|
+
};
|
|
59
|
+
exports.createQueryConfig = createQueryConfig;
|
|
40
60
|
//# sourceMappingURL=index.js.map
|
package/dist/config/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/config/index.ts"],"names":[],"mappings":";;;AA2BA;;GAEG;AACU,QAAA,aAAa,GAAkB;IAC1C,UAAU,EAAE,8CAA8C;IAC1D,aAAa,EACX,+EAA+E;IACjF,QAAQ,EAAE,OAAO;IACjB,aAAa,EAAE,CAAC;CACjB,CAAC;AAEF,IAAI,aAAa,qBAAuB,qBAAa,CAAE,CAAC;AAExD;;;GAGG;AACI,MAAM,SAAS,GAAG,CAAC,MAA8B,EAAQ,EAAE;IAChE,aAAa,mCAAQ,aAAa,GAAK,MAAM,CAAE,CAAC;AAClD,CAAC,CAAC;AAFW,QAAA,SAAS,aAEpB;AAEF;;;GAGG;AACI,MAAM,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/config/index.ts"],"names":[],"mappings":";;;AA2BA;;GAEG;AACU,QAAA,aAAa,GAAkB;IAC1C,UAAU,EAAE,8CAA8C;IAC1D,aAAa,EACX,+EAA+E;IACjF,QAAQ,EAAE,OAAO;IACjB,aAAa,EAAE,CAAC;CACjB,CAAC;AAEF,IAAI,aAAa,qBAAuB,qBAAa,CAAE,CAAC;AAExD;;;GAGG;AACI,MAAM,SAAS,GAAG,CAAC,MAA8B,EAAQ,EAAE;IAChE,aAAa,mCAAQ,aAAa,GAAK,MAAM,CAAE,CAAC;AAClD,CAAC,CAAC;AAFW,QAAA,SAAS,aAEpB;AAEF;;;GAGG;AACI,MAAM,aAAa,GAAG,GAAkB,EAAE,CAAC,aAAa,CAAC;AAAnD,QAAA,aAAa,iBAAsC;AAEhE;;;;GAIG;AACI,MAAM,MAAM,GAAG,CACpB,KAAiC,EACjC,OAAe,EACT,EAAE;IACR,MAAM,MAAM,GAAG,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC;IACxD,IAAI,MAAM,CAAC,IAAA,qBAAa,GAAE,CAAC,QAAQ,CAAC,IAAI,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;QACtD,OAAO,CAAC,KAAK,KAAK,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC;IACxD,CAAC;AACH,CAAC,CAAC;AARW,QAAA,MAAM,UAQjB;AA2CF;;GAEG;AACU,QAAA,kBAAkB,GAAsC;IACnE,KAAK,EAAE,GAAG;IACV,IAAI,EAAE,KAAK;IACX,WAAW,EAAE,GAAG;IAChB,MAAM,EAAE,QAAQ;IAChB,EAAE,EAAE,CAAC;IACL,OAAO,EAAE,sBAAsB;CAChC,CAAC;AAEF;;;;GAIG;AACI,MAAM,iBAAiB,GAAG,CAC/B,WAC4C,EACxB,EAAE;IACtB,uCAAY,0BAAkB,GAAK,WAAW,EAAG;AACnD,CAAC,CAAC;AALW,QAAA,iBAAiB,qBAK5B"}
|
package/lib/client/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { apiRequest } from './request';
|
|
2
|
-
import {
|
|
2
|
+
import { getBaseConfig, createQueryConfig } from '../config';
|
|
3
3
|
|
|
4
4
|
import type { FetchOptions } from 'ofetch';
|
|
5
5
|
import type { BlocketQueryParams, BlocketAd, BlocketResponse } from '../types';
|
|
@@ -31,9 +31,10 @@ export async function find(
|
|
|
31
31
|
query: BlocketQueryParams,
|
|
32
32
|
fetchOptions?: FetchOptions<'json', any>
|
|
33
33
|
): Promise<BlocketAd[]> {
|
|
34
|
-
const config =
|
|
34
|
+
const config = getBaseConfig();
|
|
35
|
+
const queryConfig = createQueryConfig(query);
|
|
35
36
|
const response = await apiRequest<BlocketResponse>(config.apiBaseUrl, {
|
|
36
|
-
query: remapQueryParams(
|
|
37
|
+
query: remapQueryParams(queryConfig),
|
|
37
38
|
...fetchOptions,
|
|
38
39
|
});
|
|
39
40
|
|
|
@@ -56,7 +57,7 @@ async function getAd(
|
|
|
56
57
|
adId: string,
|
|
57
58
|
fetchOptions?: FetchOptions<'json', any>
|
|
58
59
|
): Promise<BlocketAd> {
|
|
59
|
-
const config =
|
|
60
|
+
const config = getBaseConfig();
|
|
60
61
|
const url = `${config.apiBaseUrl}/ad/${adId}`;
|
|
61
62
|
|
|
62
63
|
return await apiRequest<BlocketAd>(url, fetchOptions);
|
package/lib/client/request.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ofetch, type FetchOptions } from 'ofetch';
|
|
2
2
|
|
|
3
3
|
import { fetchToken, setCachedToken } from './token';
|
|
4
|
-
import {
|
|
4
|
+
import { getBaseConfig, logger } from '../config';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Make an API request with automatic token handling and retry on 401 errors.
|
|
@@ -15,7 +15,7 @@ export async function apiRequest<T>(
|
|
|
15
15
|
options: FetchOptions<'json', any> = {},
|
|
16
16
|
retryCount: number = 0
|
|
17
17
|
): Promise<T> {
|
|
18
|
-
const config =
|
|
18
|
+
const config = getBaseConfig();
|
|
19
19
|
const token = await fetchToken();
|
|
20
20
|
|
|
21
21
|
try {
|
package/lib/client/token.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ofetch } from 'ofetch';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { getBaseConfig, logger } from '../config';
|
|
4
4
|
|
|
5
5
|
import type { BlocketAccessToken } from '../types';
|
|
6
6
|
|
|
@@ -32,7 +32,7 @@ export const fetchToken = async (
|
|
|
32
32
|
|
|
33
33
|
logger('debug', 'Fetching new Blocket API token.');
|
|
34
34
|
|
|
35
|
-
const config =
|
|
35
|
+
const config = getBaseConfig();
|
|
36
36
|
|
|
37
37
|
const tokenData = await ofetch<BlocketAccessToken>(config.tokenEndpoint);
|
|
38
38
|
if (!tokenData || !tokenData.bearerToken) {
|
package/lib/config/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Configuration interface.
|
|
2
|
+
* Blocket.js Global Configuration interface.
|
|
3
3
|
*/
|
|
4
4
|
export interface BlocketConfig {
|
|
5
5
|
/**
|
|
@@ -26,7 +26,7 @@ export interface BlocketConfig {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
|
-
* Default configuration.
|
|
29
|
+
* Default global configuration.
|
|
30
30
|
*/
|
|
31
31
|
export const defaultConfig: BlocketConfig = {
|
|
32
32
|
apiBaseUrl: 'https://api.blocket.se/search_bff/v2/content',
|
|
@@ -50,7 +50,7 @@ export const configure = (config: Partial<BlocketConfig>): void => {
|
|
|
50
50
|
* Get the current configuration.
|
|
51
51
|
* @returns The current Blocket.js configuration.
|
|
52
52
|
*/
|
|
53
|
-
export const
|
|
53
|
+
export const getBaseConfig = (): BlocketConfig => currentConfig;
|
|
54
54
|
|
|
55
55
|
/**
|
|
56
56
|
* Simple logger function based on log level.
|
|
@@ -62,7 +62,72 @@ export const logger = (
|
|
|
62
62
|
message: string
|
|
63
63
|
): void => {
|
|
64
64
|
const levels = { none: 0, error: 1, info: 2, debug: 3 };
|
|
65
|
-
if (levels[
|
|
65
|
+
if (levels[getBaseConfig().logLevel] >= levels[level]) {
|
|
66
66
|
console[level === 'error' ? 'error' : 'log'](message);
|
|
67
67
|
}
|
|
68
68
|
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Blocket Query Configuration interface.
|
|
72
|
+
* The only required property is the query string.
|
|
73
|
+
*/
|
|
74
|
+
export interface BlocketQueryConfig {
|
|
75
|
+
/**
|
|
76
|
+
* The search query string.
|
|
77
|
+
*/
|
|
78
|
+
query: string;
|
|
79
|
+
/**
|
|
80
|
+
* The maximum number of results to return.
|
|
81
|
+
* @default 100
|
|
82
|
+
*/
|
|
83
|
+
limit?: number;
|
|
84
|
+
/**
|
|
85
|
+
* The sorting order of the results.
|
|
86
|
+
* @default 'rel'
|
|
87
|
+
*/
|
|
88
|
+
sort?: 'rel';
|
|
89
|
+
/**
|
|
90
|
+
* The type of listing to search for. 's' for selling, 'b' for buying.
|
|
91
|
+
* @default 's'
|
|
92
|
+
*/
|
|
93
|
+
listingType?: 's' | 'b';
|
|
94
|
+
/**
|
|
95
|
+
* The status of the ad.
|
|
96
|
+
* @default 'active'
|
|
97
|
+
*/
|
|
98
|
+
status?: 'active' | 'inactive' | string;
|
|
99
|
+
/**
|
|
100
|
+
* The maximum distance in kilometers from the search location.
|
|
101
|
+
* @default 3
|
|
102
|
+
*/
|
|
103
|
+
gl?: number;
|
|
104
|
+
/**
|
|
105
|
+
* Additional filters or fields to include in the response.
|
|
106
|
+
* @default 'extend_with_shipping'
|
|
107
|
+
*/
|
|
108
|
+
include?: string;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Default query configuration (excluding the required query string).
|
|
113
|
+
*/
|
|
114
|
+
export const defaultQueryConfig: Omit<BlocketQueryConfig, 'query'> = {
|
|
115
|
+
limit: 100,
|
|
116
|
+
sort: 'rel',
|
|
117
|
+
listingType: 's',
|
|
118
|
+
status: 'active',
|
|
119
|
+
gl: 3,
|
|
120
|
+
include: 'extend_with_shipping',
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Merges the provided query configuration with default values.
|
|
125
|
+
* @param queryConfig Partial query configuration that must include the query string.
|
|
126
|
+
* @returns A complete BlocketQueryConfig object.
|
|
127
|
+
*/
|
|
128
|
+
export const createQueryConfig = (
|
|
129
|
+
queryConfig: Pick<BlocketQueryConfig, 'query'> &
|
|
130
|
+
Partial<Omit<BlocketQueryConfig, 'query'>>
|
|
131
|
+
): BlocketQueryConfig => {
|
|
132
|
+
return { ...defaultQueryConfig, ...queryConfig };
|
|
133
|
+
};
|