blocket.js 1.0.9 → 1.1.0
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/README.md +5 -0
- package/dist/client/index.d.ts +9 -2
- package/dist/client/index.js +32 -15
- package/dist/client/index.js.map +1 -1
- package/dist/config/index.d.ts +1 -67
- package/dist/config/index.js +0 -4
- package/dist/config/index.js.map +1 -1
- package/dist/types/config.d.ts +77 -0
- package/dist/types/config.js +3 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/index.d.ts +8 -46
- package/dist/types/index.js +15 -0
- package/dist/types/index.js.map +1 -1
- package/lib/client/index.ts +58 -23
- package/lib/config/index.ts +1 -72
- package/lib/types/config.ts +79 -0
- package/lib/types/index.ts +10 -47
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
blocket.js is a lightweight and easy-to-use npm package that provides a TypeScript interface to the unofficial Blocket API. It allows you to search and retrieve ads from blocket.se with automatic token management and error handling, so you can focus on building your application without worrying about the underlying API token management.
|
|
4
4
|
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+

|
|
9
|
+
|
|
5
10
|
## Features
|
|
6
11
|
|
|
7
12
|
- **Simple API**: Import the client and call methods like `find` directly.
|
package/dist/client/index.d.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import type { FetchOptions } from 'ofetch';
|
|
2
|
-
import type {
|
|
2
|
+
import type { BlocketAd, BlocketQueryConfig } from '../types';
|
|
3
3
|
/**
|
|
4
4
|
* Find ads on Blocket based on query parameters.
|
|
5
5
|
* @param query Blocket query parameters.
|
|
6
6
|
* @param fetchOptions Additional fetch options.
|
|
7
7
|
* @returns Array of Blocket ads.
|
|
8
8
|
*/
|
|
9
|
-
export declare function find(query:
|
|
9
|
+
export declare function find(query: BlocketQueryConfig, fetchOptions?: FetchOptions<'json', any>): Promise<BlocketAd[]>;
|
|
10
|
+
/**
|
|
11
|
+
* Get details of a specific ad by its ID.
|
|
12
|
+
* @param adId Advertisement ID.
|
|
13
|
+
* @param fetchOptions Additional fetch options.
|
|
14
|
+
* @returns {Promise<BlocketAd | null>} Blocket ad details or null if not found.
|
|
15
|
+
*/
|
|
16
|
+
export declare function findById(adId: string, fetchOptions?: FetchOptions<'json', any>): Promise<BlocketAd | null>;
|
package/dist/client/index.js
CHANGED
|
@@ -10,23 +10,34 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.find = find;
|
|
13
|
+
exports.findById = findById;
|
|
13
14
|
const request_1 = require("./request");
|
|
14
15
|
const config_1 = require("../config");
|
|
15
16
|
/**
|
|
16
|
-
* Remap
|
|
17
|
-
* @param params Blocket query parameters.
|
|
18
|
-
* @returns Remapped query parameters.
|
|
17
|
+
* Remap BlocketQueryConfig to API readable BlocketQueryParamsNative.
|
|
18
|
+
* @param params Blocket user readable query parameters.
|
|
19
|
+
* @returns Remapped API readable query parameters.
|
|
19
20
|
*/
|
|
20
21
|
function remapQueryParams(params) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
sort:
|
|
25
|
-
|
|
26
|
-
status:
|
|
27
|
-
|
|
28
|
-
include:
|
|
22
|
+
const mapping = {
|
|
23
|
+
query: 'q',
|
|
24
|
+
limit: 'lim',
|
|
25
|
+
sort: 'sort',
|
|
26
|
+
listingType: 'st',
|
|
27
|
+
status: 'status',
|
|
28
|
+
geolocation: 'gl',
|
|
29
|
+
include: 'include',
|
|
29
30
|
};
|
|
31
|
+
const remapped = {};
|
|
32
|
+
for (const key in params) {
|
|
33
|
+
if (mapping[key]) {
|
|
34
|
+
const newKey = mapping[key];
|
|
35
|
+
Object.assign(remapped, {
|
|
36
|
+
[newKey]: params[key],
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return remapped;
|
|
30
41
|
}
|
|
31
42
|
/**
|
|
32
43
|
* Find ads on Blocket based on query parameters.
|
|
@@ -36,6 +47,8 @@ function remapQueryParams(params) {
|
|
|
36
47
|
*/
|
|
37
48
|
function find(query, fetchOptions) {
|
|
38
49
|
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
if (!query.query)
|
|
51
|
+
throw new Error('Query string is required');
|
|
39
52
|
const config = (0, config_1.getBaseConfig)();
|
|
40
53
|
const queryConfig = (0, config_1.createQueryConfig)(query);
|
|
41
54
|
const response = yield (0, request_1.apiRequest)(config.apiBaseUrl, Object.assign({ query: remapQueryParams(queryConfig) }, fetchOptions));
|
|
@@ -49,13 +62,17 @@ function find(query, fetchOptions) {
|
|
|
49
62
|
* Get details of a specific ad by its ID.
|
|
50
63
|
* @param adId Advertisement ID.
|
|
51
64
|
* @param fetchOptions Additional fetch options.
|
|
52
|
-
* @returns Blocket ad details.
|
|
65
|
+
* @returns {Promise<BlocketAd | null>} Blocket ad details or null if not found.
|
|
53
66
|
*/
|
|
54
|
-
function
|
|
67
|
+
function findById(adId, fetchOptions) {
|
|
55
68
|
return __awaiter(this, void 0, void 0, function* () {
|
|
56
69
|
const config = (0, config_1.getBaseConfig)();
|
|
57
|
-
const url = `${config.apiBaseUrl}
|
|
58
|
-
|
|
70
|
+
const url = `${config.apiBaseUrl}/${adId}`;
|
|
71
|
+
const ad = yield (0, request_1.apiRequest)(url, fetchOptions);
|
|
72
|
+
if (!ad || !(ad === null || ad === void 0 ? void 0 : ad.data)) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
return ad.data;
|
|
59
76
|
});
|
|
60
77
|
}
|
|
61
78
|
//# sourceMappingURL=index.js.map
|
package/dist/client/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/client/index.ts"],"names":[],"mappings":";;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/client/index.ts"],"names":[],"mappings":";;;;;;;;;;;AAqDA,oBAwBC;AAQD,4BAcC;AAnGD,uCAAuC;AACvC,sCAA6D;AAW7D;;;;GAIG;AACH,SAAS,gBAAgB,CACvB,MAA0B;IAE1B,MAAM,OAAO,GAGT;QACF,KAAK,EAAE,GAAG;QACV,KAAK,EAAE,KAAK;QACZ,IAAI,EAAE,MAAM;QACZ,WAAW,EAAE,IAAI;QACjB,MAAM,EAAE,QAAQ;QAChB,WAAW,EAAE,IAAI;QACjB,OAAO,EAAE,SAAS;KACnB,CAAC;IAEF,MAAM,QAAQ,GAAsC,EAAE,CAAC;IAEvD,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;QACzB,IAAI,OAAO,CAAC,GAA+B,CAAC,EAAE,CAAC;YAC7C,MAAM,MAAM,GAAG,OAAO,CAAC,GAA+B,CAAC,CAAC;YACxD,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE;gBACtB,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,GAA+B,CAAC;aAClD,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED,OAAO,QAAoC,CAAC;AAC9C,CAAC;AAED;;;;;GAKG;AACH,SAAsB,IAAI,CACxB,KAAyB,EACzB,YAAwC;;QAExC,IAAI,CAAC,KAAK,CAAC,KAAK;YAAE,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;QAE9D,MAAM,MAAM,GAAG,IAAA,sBAAa,GAAE,CAAC;QAC/B,MAAM,WAAW,GAAG,IAAA,0BAAiB,EAAC,KAAK,CAAC,CAAC;QAE7C,MAAM,QAAQ,GAAG,MAAM,IAAA,oBAAU,EAC/B,MAAM,CAAC,UAAU,kBAEf,KAAK,EAAE,gBAAgB,CAAC,WAAW,CAAC,IACjC,YAAY,EAElB,CAAC;QAEF,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,SAAsB,QAAQ,CAC5B,IAAY,EACZ,YAAwC;;QAExC,MAAM,MAAM,GAAG,IAAA,sBAAa,GAAE,CAAC;QAC/B,MAAM,GAAG,GAAG,GAAG,MAAM,CAAC,UAAU,IAAI,IAAI,EAAE,CAAC;QAE3C,MAAM,EAAE,GAAG,MAAM,IAAA,oBAAU,EAAoB,GAAG,EAAE,YAAY,CAAC,CAAC;QAElE,IAAI,CAAC,EAAE,IAAI,CAAC,CAAA,EAAE,aAAF,EAAE,uBAAF,EAAE,CAAE,IAAI,CAAA,EAAE,CAAC;YACrB,OAAO,IAAI,CAAC;QACd,CAAC;QAED,OAAO,EAAE,CAAC,IAAI,CAAC;IACjB,CAAC;CAAA"}
|
package/dist/config/index.d.ts
CHANGED
|
@@ -1,29 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* Blocket.js Global Configuration interface.
|
|
3
|
-
*/
|
|
4
|
-
export interface BlocketConfig {
|
|
5
|
-
/**
|
|
6
|
-
* Base URL for the Blocket API.
|
|
7
|
-
* @default 'https://api.blocket.se/search_bff/v2/content'
|
|
8
|
-
*/
|
|
9
|
-
apiBaseUrl: string;
|
|
10
|
-
/**
|
|
11
|
-
* Endpoint URL to fetch the token.
|
|
12
|
-
* @default 'https://www.blocket.se/api/adout-api-route/refresh-token-and-validate-session'
|
|
13
|
-
*/
|
|
14
|
-
tokenEndpoint: string;
|
|
15
|
-
/**
|
|
16
|
-
* Log level for debugging.
|
|
17
|
-
* Options: 'none', 'error', 'info', 'debug'
|
|
18
|
-
* @default 'error'
|
|
19
|
-
*/
|
|
20
|
-
logLevel: 'none' | 'error' | 'info' | 'debug';
|
|
21
|
-
/**
|
|
22
|
-
* Maximum number of retry attempts on 401 error.
|
|
23
|
-
* @default 3
|
|
24
|
-
*/
|
|
25
|
-
retryAttempts: number;
|
|
26
|
-
}
|
|
1
|
+
import { BlocketQueryConfig, BlocketConfig } from '../types';
|
|
27
2
|
/**
|
|
28
3
|
* Default global configuration.
|
|
29
4
|
*/
|
|
@@ -44,47 +19,6 @@ export declare const getBaseConfig: () => BlocketConfig;
|
|
|
44
19
|
* @param message Message to log.
|
|
45
20
|
*/
|
|
46
21
|
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 20
|
|
59
|
-
* @max 60
|
|
60
|
-
*/
|
|
61
|
-
limit?: number;
|
|
62
|
-
/**
|
|
63
|
-
* The sorting order of the results.
|
|
64
|
-
* @default 'rel'
|
|
65
|
-
*/
|
|
66
|
-
sort?: 'rel';
|
|
67
|
-
/**
|
|
68
|
-
* The type of listing to search for. 's' for selling, 'b' for buying.
|
|
69
|
-
* @default 's'
|
|
70
|
-
*/
|
|
71
|
-
listingType?: 's' | 'b';
|
|
72
|
-
/**
|
|
73
|
-
* The status of the ad.
|
|
74
|
-
* @default 'active'
|
|
75
|
-
*/
|
|
76
|
-
status?: 'active' | 'inactive' | string;
|
|
77
|
-
/**
|
|
78
|
-
* The maximum distance in kilometers from the search location.
|
|
79
|
-
* @default 3
|
|
80
|
-
*/
|
|
81
|
-
gl?: number;
|
|
82
|
-
/**
|
|
83
|
-
* Additional filters or fields to include in the response.
|
|
84
|
-
* @default 'extend_with_shipping'
|
|
85
|
-
*/
|
|
86
|
-
include?: string;
|
|
87
|
-
}
|
|
88
22
|
/**
|
|
89
23
|
* Default query configuration (excluding the required query string).
|
|
90
24
|
*/
|
package/dist/config/index.js
CHANGED
|
@@ -41,12 +41,8 @@ exports.logger = logger;
|
|
|
41
41
|
* Default query configuration (excluding the required query string).
|
|
42
42
|
*/
|
|
43
43
|
exports.defaultQueryConfig = {
|
|
44
|
-
limit: 20,
|
|
45
|
-
sort: 'rel',
|
|
46
44
|
listingType: 's',
|
|
47
45
|
status: 'active',
|
|
48
|
-
gl: 3,
|
|
49
|
-
include: 'extend_with_shipping',
|
|
50
46
|
};
|
|
51
47
|
/**
|
|
52
48
|
* Merges the provided query configuration with default values.
|
package/dist/config/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/config/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/config/index.ts"],"names":[],"mappings":";;;AAEA;;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;AAEF;;GAEG;AACU,QAAA,kBAAkB,GAAsC;IACnE,WAAW,EAAE,GAAG;IAChB,MAAM,EAAE,QAAQ;CACjB,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"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Blocket.js Global Configuration interface.
|
|
3
|
+
*/
|
|
4
|
+
export interface BlocketConfig {
|
|
5
|
+
/**
|
|
6
|
+
* Base URL for the Blocket API.
|
|
7
|
+
* @default 'https://api.blocket.se/search_bff/v2/content'
|
|
8
|
+
*/
|
|
9
|
+
apiBaseUrl: string;
|
|
10
|
+
/**
|
|
11
|
+
* Endpoint URL to fetch the token.
|
|
12
|
+
* @default 'https://www.blocket.se/api/adout-api-route/refresh-token-and-validate-session'
|
|
13
|
+
*/
|
|
14
|
+
tokenEndpoint: string;
|
|
15
|
+
/**
|
|
16
|
+
* Log level for debugging.
|
|
17
|
+
* Options: 'none', 'error', 'info', 'debug'
|
|
18
|
+
* @default 'error'
|
|
19
|
+
*/
|
|
20
|
+
logLevel: 'none' | 'error' | 'info' | 'debug';
|
|
21
|
+
/**
|
|
22
|
+
* Maximum number of retry attempts on 401 error.
|
|
23
|
+
* @default 3
|
|
24
|
+
*/
|
|
25
|
+
retryAttempts: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Blocket Query Configuration interface.
|
|
29
|
+
* The only required property is the query string.
|
|
30
|
+
*/
|
|
31
|
+
export interface BlocketQueryConfig {
|
|
32
|
+
/**
|
|
33
|
+
* The search query string.
|
|
34
|
+
*/
|
|
35
|
+
query: string;
|
|
36
|
+
/**
|
|
37
|
+
* The maximum number of results to return.
|
|
38
|
+
* Value is omitted by default but defaults to 20 by the API.
|
|
39
|
+
* @default 20
|
|
40
|
+
* @max 60
|
|
41
|
+
*/
|
|
42
|
+
limit?: number;
|
|
43
|
+
/**
|
|
44
|
+
* The sorting order of the results.
|
|
45
|
+
*/
|
|
46
|
+
sort?: 'rel';
|
|
47
|
+
/**
|
|
48
|
+
* The type of listing to search for. `s` for selling, `b` for buying, `a` for all.
|
|
49
|
+
* @default 's'
|
|
50
|
+
*/
|
|
51
|
+
listingType?: 's' | 'b' | 'a';
|
|
52
|
+
/**
|
|
53
|
+
* The status of the ad. `active`, `inactive`, or `all`.
|
|
54
|
+
* @default 'active'
|
|
55
|
+
*/
|
|
56
|
+
status?: 'active' | 'inactive' | 'all';
|
|
57
|
+
/**
|
|
58
|
+
* The maximum distance in kilometers from the search location.
|
|
59
|
+
*/
|
|
60
|
+
geolocation?: number;
|
|
61
|
+
/**
|
|
62
|
+
* Additional filters or fields to include in the response.
|
|
63
|
+
*/
|
|
64
|
+
include?: 'extend_with_shipping' | string;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Native Blocket query parameters for API requests.
|
|
68
|
+
*/
|
|
69
|
+
export interface BlocketQueryParamsNative {
|
|
70
|
+
q: string;
|
|
71
|
+
lim?: number;
|
|
72
|
+
sort?: 'rel';
|
|
73
|
+
st?: 's' | 'b' | 'a';
|
|
74
|
+
status?: 'active' | 'inactive' | 'all';
|
|
75
|
+
gl?: number;
|
|
76
|
+
include?: string;
|
|
77
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../lib/types/config.ts"],"names":[],"mappings":""}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export * from './config';
|
|
1
2
|
/**
|
|
2
3
|
* Access token.
|
|
3
4
|
*/
|
|
@@ -9,9 +10,15 @@ export type BlocketAccessToken = {
|
|
|
9
10
|
/**
|
|
10
11
|
* Blocket API response containing an array of ads.
|
|
11
12
|
*/
|
|
12
|
-
export interface
|
|
13
|
+
export interface BlocketAdSearchResponse {
|
|
13
14
|
data: BlocketAd[];
|
|
14
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Blocket API response containing a single ad.
|
|
18
|
+
*/
|
|
19
|
+
export interface BlocketAdResponse {
|
|
20
|
+
data: BlocketAd;
|
|
21
|
+
}
|
|
15
22
|
/**
|
|
16
23
|
* Blocket advertisement object.
|
|
17
24
|
*/
|
|
@@ -53,48 +60,3 @@ export interface BlocketAd {
|
|
|
53
60
|
type: string;
|
|
54
61
|
zipcode: string;
|
|
55
62
|
}
|
|
56
|
-
/**
|
|
57
|
-
* Parameters for querying Blocket ads.
|
|
58
|
-
*/
|
|
59
|
-
export interface BlocketQueryParams {
|
|
60
|
-
/**
|
|
61
|
-
* The search query.
|
|
62
|
-
* @example 'macbook air'
|
|
63
|
-
*/
|
|
64
|
-
query: string;
|
|
65
|
-
/**
|
|
66
|
-
* The maximum number of results to return.
|
|
67
|
-
* @example 10
|
|
68
|
-
* @default 60
|
|
69
|
-
*/
|
|
70
|
-
limit?: number;
|
|
71
|
-
/**
|
|
72
|
-
* The sorting order of the results.
|
|
73
|
-
* @example 'rel'
|
|
74
|
-
* @default 'rel'
|
|
75
|
-
*/
|
|
76
|
-
sort?: 'rel';
|
|
77
|
-
/**
|
|
78
|
-
* The type of listing to search for. 's' for selling, 'b' for buying.
|
|
79
|
-
* @example 's'
|
|
80
|
-
* @default 's'
|
|
81
|
-
* @options 's' | 'b'
|
|
82
|
-
*/
|
|
83
|
-
listingType?: 's' | 'b';
|
|
84
|
-
/**
|
|
85
|
-
* The status of the ad.
|
|
86
|
-
* @example 'active'
|
|
87
|
-
* @default 'active'
|
|
88
|
-
*/
|
|
89
|
-
status?: 'active' | 'inactive' | string;
|
|
90
|
-
/**
|
|
91
|
-
* The maximum distance in kilometers from the search location.
|
|
92
|
-
* @example 10
|
|
93
|
-
*/
|
|
94
|
-
gl?: number;
|
|
95
|
-
/**
|
|
96
|
-
* Additional filters or fields to include in the response.
|
|
97
|
-
* @example 'image,description'
|
|
98
|
-
*/
|
|
99
|
-
include?: string;
|
|
100
|
-
}
|
package/dist/types/index.js
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./config"), exports);
|
|
3
18
|
//# sourceMappingURL=index.js.map
|
package/dist/types/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/types/index.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB"}
|
package/lib/client/index.ts
CHANGED
|
@@ -2,23 +2,47 @@ import { apiRequest } from './request';
|
|
|
2
2
|
import { getBaseConfig, createQueryConfig } from '../config';
|
|
3
3
|
|
|
4
4
|
import type { FetchOptions } from 'ofetch';
|
|
5
|
-
import type {
|
|
5
|
+
import type {
|
|
6
|
+
BlocketQueryParamsNative,
|
|
7
|
+
BlocketAd,
|
|
8
|
+
BlocketAdResponse,
|
|
9
|
+
BlocketAdSearchResponse,
|
|
10
|
+
BlocketQueryConfig,
|
|
11
|
+
} from '../types';
|
|
6
12
|
|
|
7
13
|
/**
|
|
8
|
-
* Remap
|
|
9
|
-
* @param params Blocket query parameters.
|
|
10
|
-
* @returns Remapped query parameters.
|
|
14
|
+
* Remap BlocketQueryConfig to API readable BlocketQueryParamsNative.
|
|
15
|
+
* @param params Blocket user readable query parameters.
|
|
16
|
+
* @returns Remapped API readable query parameters.
|
|
11
17
|
*/
|
|
12
|
-
function remapQueryParams(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
function remapQueryParams(
|
|
19
|
+
params: BlocketQueryConfig
|
|
20
|
+
): BlocketQueryParamsNative {
|
|
21
|
+
const mapping: Record<
|
|
22
|
+
keyof BlocketQueryConfig,
|
|
23
|
+
keyof BlocketQueryParamsNative
|
|
24
|
+
> = {
|
|
25
|
+
query: 'q',
|
|
26
|
+
limit: 'lim',
|
|
27
|
+
sort: 'sort',
|
|
28
|
+
listingType: 'st',
|
|
29
|
+
status: 'status',
|
|
30
|
+
geolocation: 'gl',
|
|
31
|
+
include: 'include',
|
|
21
32
|
};
|
|
33
|
+
|
|
34
|
+
const remapped: Partial<BlocketQueryParamsNative> = {};
|
|
35
|
+
|
|
36
|
+
for (const key in params) {
|
|
37
|
+
if (mapping[key as keyof BlocketQueryConfig]) {
|
|
38
|
+
const newKey = mapping[key as keyof BlocketQueryConfig];
|
|
39
|
+
Object.assign(remapped, {
|
|
40
|
+
[newKey]: params[key as keyof BlocketQueryConfig],
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return remapped as BlocketQueryParamsNative;
|
|
22
46
|
}
|
|
23
47
|
|
|
24
48
|
/**
|
|
@@ -28,16 +52,21 @@ function remapQueryParams(params: BlocketQueryParams): Record<string, any> {
|
|
|
28
52
|
* @returns Array of Blocket ads.
|
|
29
53
|
*/
|
|
30
54
|
export async function find(
|
|
31
|
-
query:
|
|
55
|
+
query: BlocketQueryConfig,
|
|
32
56
|
fetchOptions?: FetchOptions<'json', any>
|
|
33
57
|
): Promise<BlocketAd[]> {
|
|
58
|
+
if (!query.query) throw new Error('Query string is required');
|
|
59
|
+
|
|
34
60
|
const config = getBaseConfig();
|
|
35
61
|
const queryConfig = createQueryConfig(query);
|
|
36
62
|
|
|
37
|
-
const response = await apiRequest<
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
63
|
+
const response = await apiRequest<BlocketAdSearchResponse>(
|
|
64
|
+
config.apiBaseUrl,
|
|
65
|
+
{
|
|
66
|
+
query: remapQueryParams(queryConfig),
|
|
67
|
+
...fetchOptions,
|
|
68
|
+
}
|
|
69
|
+
);
|
|
41
70
|
|
|
42
71
|
if (!response || !response.data || !Array.isArray(response.data)) {
|
|
43
72
|
throw new Error(
|
|
@@ -52,14 +81,20 @@ export async function find(
|
|
|
52
81
|
* Get details of a specific ad by its ID.
|
|
53
82
|
* @param adId Advertisement ID.
|
|
54
83
|
* @param fetchOptions Additional fetch options.
|
|
55
|
-
* @returns Blocket ad details.
|
|
84
|
+
* @returns {Promise<BlocketAd | null>} Blocket ad details or null if not found.
|
|
56
85
|
*/
|
|
57
|
-
async function
|
|
86
|
+
export async function findById(
|
|
58
87
|
adId: string,
|
|
59
88
|
fetchOptions?: FetchOptions<'json', any>
|
|
60
|
-
): Promise<BlocketAd> {
|
|
89
|
+
): Promise<BlocketAd | null> {
|
|
61
90
|
const config = getBaseConfig();
|
|
62
|
-
const url = `${config.apiBaseUrl}
|
|
91
|
+
const url = `${config.apiBaseUrl}/${adId}`;
|
|
92
|
+
|
|
93
|
+
const ad = await apiRequest<BlocketAdResponse>(url, fetchOptions);
|
|
94
|
+
|
|
95
|
+
if (!ad || !ad?.data) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
63
98
|
|
|
64
|
-
return
|
|
99
|
+
return ad.data;
|
|
65
100
|
}
|
package/lib/config/index.ts
CHANGED
|
@@ -1,29 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* Blocket.js Global Configuration interface.
|
|
3
|
-
*/
|
|
4
|
-
export interface BlocketConfig {
|
|
5
|
-
/**
|
|
6
|
-
* Base URL for the Blocket API.
|
|
7
|
-
* @default 'https://api.blocket.se/search_bff/v2/content'
|
|
8
|
-
*/
|
|
9
|
-
apiBaseUrl: string;
|
|
10
|
-
/**
|
|
11
|
-
* Endpoint URL to fetch the token.
|
|
12
|
-
* @default 'https://www.blocket.se/api/adout-api-route/refresh-token-and-validate-session'
|
|
13
|
-
*/
|
|
14
|
-
tokenEndpoint: string;
|
|
15
|
-
/**
|
|
16
|
-
* Log level for debugging.
|
|
17
|
-
* Options: 'none', 'error', 'info', 'debug'
|
|
18
|
-
* @default 'error'
|
|
19
|
-
*/
|
|
20
|
-
logLevel: 'none' | 'error' | 'info' | 'debug';
|
|
21
|
-
/**
|
|
22
|
-
* Maximum number of retry attempts on 401 error.
|
|
23
|
-
* @default 3
|
|
24
|
-
*/
|
|
25
|
-
retryAttempts: number;
|
|
26
|
-
}
|
|
1
|
+
import { BlocketQueryConfig, BlocketConfig } from '../types';
|
|
27
2
|
|
|
28
3
|
/**
|
|
29
4
|
* Default global configuration.
|
|
@@ -67,58 +42,12 @@ export const logger = (
|
|
|
67
42
|
}
|
|
68
43
|
};
|
|
69
44
|
|
|
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 20
|
|
82
|
-
* @max 60
|
|
83
|
-
*/
|
|
84
|
-
limit?: number;
|
|
85
|
-
/**
|
|
86
|
-
* The sorting order of the results.
|
|
87
|
-
* @default 'rel'
|
|
88
|
-
*/
|
|
89
|
-
sort?: 'rel';
|
|
90
|
-
/**
|
|
91
|
-
* The type of listing to search for. 's' for selling, 'b' for buying.
|
|
92
|
-
* @default 's'
|
|
93
|
-
*/
|
|
94
|
-
listingType?: 's' | 'b';
|
|
95
|
-
/**
|
|
96
|
-
* The status of the ad.
|
|
97
|
-
* @default 'active'
|
|
98
|
-
*/
|
|
99
|
-
status?: 'active' | 'inactive' | string;
|
|
100
|
-
/**
|
|
101
|
-
* The maximum distance in kilometers from the search location.
|
|
102
|
-
* @default 3
|
|
103
|
-
*/
|
|
104
|
-
gl?: number;
|
|
105
|
-
/**
|
|
106
|
-
* Additional filters or fields to include in the response.
|
|
107
|
-
* @default 'extend_with_shipping'
|
|
108
|
-
*/
|
|
109
|
-
include?: string;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
45
|
/**
|
|
113
46
|
* Default query configuration (excluding the required query string).
|
|
114
47
|
*/
|
|
115
48
|
export const defaultQueryConfig: Omit<BlocketQueryConfig, 'query'> = {
|
|
116
|
-
limit: 20,
|
|
117
|
-
sort: 'rel',
|
|
118
49
|
listingType: 's',
|
|
119
50
|
status: 'active',
|
|
120
|
-
gl: 3,
|
|
121
|
-
include: 'extend_with_shipping',
|
|
122
51
|
};
|
|
123
52
|
|
|
124
53
|
/**
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Blocket.js Global Configuration interface.
|
|
3
|
+
*/
|
|
4
|
+
export interface BlocketConfig {
|
|
5
|
+
/**
|
|
6
|
+
* Base URL for the Blocket API.
|
|
7
|
+
* @default 'https://api.blocket.se/search_bff/v2/content'
|
|
8
|
+
*/
|
|
9
|
+
apiBaseUrl: string;
|
|
10
|
+
/**
|
|
11
|
+
* Endpoint URL to fetch the token.
|
|
12
|
+
* @default 'https://www.blocket.se/api/adout-api-route/refresh-token-and-validate-session'
|
|
13
|
+
*/
|
|
14
|
+
tokenEndpoint: string;
|
|
15
|
+
/**
|
|
16
|
+
* Log level for debugging.
|
|
17
|
+
* Options: 'none', 'error', 'info', 'debug'
|
|
18
|
+
* @default 'error'
|
|
19
|
+
*/
|
|
20
|
+
logLevel: 'none' | 'error' | 'info' | 'debug';
|
|
21
|
+
/**
|
|
22
|
+
* Maximum number of retry attempts on 401 error.
|
|
23
|
+
* @default 3
|
|
24
|
+
*/
|
|
25
|
+
retryAttempts: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Blocket Query Configuration interface.
|
|
30
|
+
* The only required property is the query string.
|
|
31
|
+
*/
|
|
32
|
+
export interface BlocketQueryConfig {
|
|
33
|
+
/**
|
|
34
|
+
* The search query string.
|
|
35
|
+
*/
|
|
36
|
+
query: string;
|
|
37
|
+
/**
|
|
38
|
+
* The maximum number of results to return.
|
|
39
|
+
* Value is omitted by default but defaults to 20 by the API.
|
|
40
|
+
* @default 20
|
|
41
|
+
* @max 60
|
|
42
|
+
*/
|
|
43
|
+
limit?: number;
|
|
44
|
+
/**
|
|
45
|
+
* The sorting order of the results.
|
|
46
|
+
*/
|
|
47
|
+
sort?: 'rel';
|
|
48
|
+
/**
|
|
49
|
+
* The type of listing to search for. `s` for selling, `b` for buying, `a` for all.
|
|
50
|
+
* @default 's'
|
|
51
|
+
*/
|
|
52
|
+
listingType?: 's' | 'b' | 'a';
|
|
53
|
+
/**
|
|
54
|
+
* The status of the ad. `active`, `inactive`, or `all`.
|
|
55
|
+
* @default 'active'
|
|
56
|
+
*/
|
|
57
|
+
status?: 'active' | 'inactive' | 'all';
|
|
58
|
+
/**
|
|
59
|
+
* The maximum distance in kilometers from the search location.
|
|
60
|
+
*/
|
|
61
|
+
geolocation?: number;
|
|
62
|
+
/**
|
|
63
|
+
* Additional filters or fields to include in the response.
|
|
64
|
+
*/
|
|
65
|
+
include?: 'extend_with_shipping' | string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Native Blocket query parameters for API requests.
|
|
70
|
+
*/
|
|
71
|
+
export interface BlocketQueryParamsNative {
|
|
72
|
+
q: string;
|
|
73
|
+
lim?: number;
|
|
74
|
+
sort?: 'rel';
|
|
75
|
+
st?: 's' | 'b' | 'a';
|
|
76
|
+
status?: 'active' | 'inactive' | 'all';
|
|
77
|
+
gl?: number;
|
|
78
|
+
include?: string;
|
|
79
|
+
}
|
package/lib/types/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export * from './config';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Access token.
|
|
3
5
|
*/
|
|
@@ -10,10 +12,17 @@ export type BlocketAccessToken = {
|
|
|
10
12
|
/**
|
|
11
13
|
* Blocket API response containing an array of ads.
|
|
12
14
|
*/
|
|
13
|
-
export interface
|
|
15
|
+
export interface BlocketAdSearchResponse {
|
|
14
16
|
data: BlocketAd[];
|
|
15
17
|
}
|
|
16
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Blocket API response containing a single ad.
|
|
21
|
+
*/
|
|
22
|
+
export interface BlocketAdResponse {
|
|
23
|
+
data: BlocketAd;
|
|
24
|
+
}
|
|
25
|
+
|
|
17
26
|
/**
|
|
18
27
|
* Blocket advertisement object.
|
|
19
28
|
*/
|
|
@@ -55,49 +64,3 @@ export interface BlocketAd {
|
|
|
55
64
|
type: string;
|
|
56
65
|
zipcode: string;
|
|
57
66
|
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Parameters for querying Blocket ads.
|
|
61
|
-
*/
|
|
62
|
-
export interface BlocketQueryParams {
|
|
63
|
-
/**
|
|
64
|
-
* The search query.
|
|
65
|
-
* @example 'macbook air'
|
|
66
|
-
*/
|
|
67
|
-
query: string;
|
|
68
|
-
/**
|
|
69
|
-
* The maximum number of results to return.
|
|
70
|
-
* @example 10
|
|
71
|
-
* @default 60
|
|
72
|
-
*/
|
|
73
|
-
limit?: number;
|
|
74
|
-
/**
|
|
75
|
-
* The sorting order of the results.
|
|
76
|
-
* @example 'rel'
|
|
77
|
-
* @default 'rel'
|
|
78
|
-
*/
|
|
79
|
-
sort?: 'rel';
|
|
80
|
-
/**
|
|
81
|
-
* The type of listing to search for. 's' for selling, 'b' for buying.
|
|
82
|
-
* @example 's'
|
|
83
|
-
* @default 's'
|
|
84
|
-
* @options 's' | 'b'
|
|
85
|
-
*/
|
|
86
|
-
listingType?: 's' | 'b';
|
|
87
|
-
/**
|
|
88
|
-
* The status of the ad.
|
|
89
|
-
* @example 'active'
|
|
90
|
-
* @default 'active'
|
|
91
|
-
*/
|
|
92
|
-
status?: 'active' | 'inactive' | string;
|
|
93
|
-
/**
|
|
94
|
-
* The maximum distance in kilometers from the search location.
|
|
95
|
-
* @example 10
|
|
96
|
-
*/
|
|
97
|
-
gl?: number;
|
|
98
|
-
/**
|
|
99
|
-
* Additional filters or fields to include in the response.
|
|
100
|
-
* @example 'image,description'
|
|
101
|
-
*/
|
|
102
|
-
include?: string;
|
|
103
|
-
}
|