blocket.js 1.0.9 → 1.0.10

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 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
+ ![NPM Version](https://img.shields.io/npm/v/blocket.js)
6
+ ![NPM Downloads](https://img.shields.io/npm/dm/blocket.js)
7
+ ![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/rutbergphilip/blocket.js/publish.yaml)
8
+ ![NPM Unpacked Size](https://img.shields.io/npm/unpacked-size/blocket.js)
9
+
5
10
  ## Features
6
11
 
7
12
  - **Simple API**: Import the client and call methods like `find` directly.
@@ -1,9 +1,9 @@
1
1
  import type { FetchOptions } from 'ofetch';
2
- import type { BlocketQueryParams, BlocketAd } from '../types';
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: BlocketQueryParams, fetchOptions?: FetchOptions<'json', any>): Promise<BlocketAd[]>;
9
+ export declare function find(query: BlocketQueryConfig, fetchOptions?: FetchOptions<'json', any>): Promise<BlocketAd[]>;
@@ -13,20 +13,30 @@ exports.find = find;
13
13
  const request_1 = require("./request");
14
14
  const config_1 = require("../config");
15
15
  /**
16
- * Remap BlocketQueryParams to API query parameters.
17
- * @param params Blocket query parameters.
18
- * @returns Remapped query parameters.
16
+ * Remap BlocketQueryConfig to API readable BlocketQueryParamsNative.
17
+ * @param params Blocket user readable query parameters.
18
+ * @returns Remapped API readable query parameters.
19
19
  */
20
20
  function remapQueryParams(params) {
21
- return {
22
- q: params.query,
23
- lim: params.limit,
24
- sort: params.sort,
25
- st: params.listingType,
26
- status: params.status,
27
- gl: params.gl,
28
- include: params.include,
21
+ const mapping = {
22
+ query: 'q',
23
+ limit: 'lim',
24
+ sort: 'sort',
25
+ listingType: 'st',
26
+ status: 'status',
27
+ geolocation: 'gl',
28
+ include: 'include',
29
29
  };
30
+ const remapped = {};
31
+ for (const key in params) {
32
+ if (mapping[key]) {
33
+ const newKey = mapping[key];
34
+ Object.assign(remapped, {
35
+ [newKey]: params[key],
36
+ });
37
+ }
38
+ }
39
+ return remapped;
30
40
  }
31
41
  /**
32
42
  * Find ads on Blocket based on query parameters.
@@ -36,6 +46,8 @@ function remapQueryParams(params) {
36
46
  */
37
47
  function find(query, fetchOptions) {
38
48
  return __awaiter(this, void 0, void 0, function* () {
49
+ if (!query.query)
50
+ throw new Error('Query string is required');
39
51
  const config = (0, config_1.getBaseConfig)();
40
52
  const queryConfig = (0, config_1.createQueryConfig)(query);
41
53
  const response = yield (0, request_1.apiRequest)(config.apiBaseUrl, Object.assign({ query: remapQueryParams(queryConfig) }, fetchOptions));
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/client/index.ts"],"names":[],"mappings":";;;;;;;;;;;AA6BA,oBAmBC;AAhDD,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,G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AM,IAAA,oBAAU,EAAY,GAAG,EAAE,YAAY,CAAC,CAAC;IACxD,CAAC;CAAA"}
@@ -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
  */
@@ -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.
@@ -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,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;AAC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AO,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,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../../lib/types/config.ts"],"names":[],"mappings":""}
@@ -1,3 +1,4 @@
1
+ export * from './config';
1
2
  /**
2
3
  * Access token.
3
4
  */
@@ -53,48 +54,3 @@ export interface BlocketAd {
53
54
  type: string;
54
55
  zipcode: string;
55
56
  }
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
- }
@@ -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
@@ -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"}
@@ -2,23 +2,46 @@ import { apiRequest } from './request';
2
2
  import { getBaseConfig, createQueryConfig } from '../config';
3
3
 
4
4
  import type { FetchOptions } from 'ofetch';
5
- import type { BlocketQueryParams, BlocketAd, BlocketResponse } from '../types';
5
+ import type {
6
+ BlocketQueryParamsNative,
7
+ BlocketAd,
8
+ BlocketResponse,
9
+ BlocketQueryConfig,
10
+ } from '../types';
6
11
 
7
12
  /**
8
- * Remap BlocketQueryParams to API query parameters.
9
- * @param params Blocket query parameters.
10
- * @returns Remapped query parameters.
13
+ * Remap BlocketQueryConfig to API readable BlocketQueryParamsNative.
14
+ * @param params Blocket user readable query parameters.
15
+ * @returns Remapped API readable query parameters.
11
16
  */
12
- function remapQueryParams(params: BlocketQueryParams): Record<string, any> {
13
- return {
14
- q: params.query,
15
- lim: params.limit,
16
- sort: params.sort,
17
- st: params.listingType,
18
- status: params.status,
19
- gl: params.gl,
20
- include: params.include,
17
+ function remapQueryParams(
18
+ params: BlocketQueryConfig
19
+ ): BlocketQueryParamsNative {
20
+ const mapping: Record<
21
+ keyof BlocketQueryConfig,
22
+ keyof BlocketQueryParamsNative
23
+ > = {
24
+ query: 'q',
25
+ limit: 'lim',
26
+ sort: 'sort',
27
+ listingType: 'st',
28
+ status: 'status',
29
+ geolocation: 'gl',
30
+ include: 'include',
21
31
  };
32
+
33
+ const remapped: Partial<BlocketQueryParamsNative> = {};
34
+
35
+ for (const key in params) {
36
+ if (mapping[key as keyof BlocketQueryConfig]) {
37
+ const newKey = mapping[key as keyof BlocketQueryConfig];
38
+ Object.assign(remapped, {
39
+ [newKey]: params[key as keyof BlocketQueryConfig],
40
+ });
41
+ }
42
+ }
43
+
44
+ return remapped as BlocketQueryParamsNative;
22
45
  }
23
46
 
24
47
  /**
@@ -28,9 +51,11 @@ function remapQueryParams(params: BlocketQueryParams): Record<string, any> {
28
51
  * @returns Array of Blocket ads.
29
52
  */
30
53
  export async function find(
31
- query: BlocketQueryParams,
54
+ query: BlocketQueryConfig,
32
55
  fetchOptions?: FetchOptions<'json', any>
33
56
  ): Promise<BlocketAd[]> {
57
+ if (!query.query) throw new Error('Query string is required');
58
+
34
59
  const config = getBaseConfig();
35
60
  const queryConfig = createQueryConfig(query);
36
61
 
@@ -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
+ }
@@ -1,3 +1,5 @@
1
+ export * from './config';
2
+
1
3
  /**
2
4
  * Access token.
3
5
  */
@@ -55,49 +57,3 @@ export interface BlocketAd {
55
57
  type: string;
56
58
  zipcode: string;
57
59
  }
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
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blocket.js",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "A user-friendly js wrapper for blocket.se",
5
5
  "keywords": [
6
6
  "blocket",