-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
173 lines (157 loc) · 3.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const { createInstance } = require('@salling-group/auth');
const Traverser = require('@salling-group/pagination-traverser');
const pkg = require('./package');
/**
* A query builder for querying stores.
*/
class StoresQuery {
/**
* Constructs a new query builder.
* @param {StoresSDK} storesAPI A handle to Index.
*/
constructor(storesAPI) {
this.storesAPI = storesAPI;
this.params = {};
}
/**
* Sets a parameter for the request being built.
*
* @param {string} param The param name.
* @param {*} value The value to set.
*/
set(param, value) {
this.params[param] = value;
}
/**
* Returns only the given fields.
*
* @param {string} fields The fields to return.
* @returns {StoresQuery}
*/
pick(...fields) {
this.params.fields = fields.join(',');
return this;
}
/**
* Returns only stores of the given brand.
*
* @param {string} brand The brand to filter by.
* @returns {StoresQuery}
*/
ofBrand(brand) {
this.set('brand', brand);
return this;
}
/**
* Returns only stores in the given city.
*
* @param {string} city The city to filter by.
* @returns {StoresQuery}
*/
inCity(city) {
this.set('city', city);
return this;
}
/**
* Returns only stores in the given ZIP code.
*
* @param {string} zip The ZIP code to filter by.
* @returns {StoresQuery}
*/
inZIP(zip) {
this.set('zip', zip);
return this;
}
/**
* Returns only stores near a given coordinate.
*
* @param {number|string} long The longitude of the coordinate.
* @param {number|string} lat The latitude of the coordinate.
* @param {number|string} radius The radius around the coordinate in kilometers.
* @returns {StoresQuery}
*/
nearCoordinate(long, lat, radius) {
this.set('geo', `${long},${lat}`);
this.set('radius', radius);
return this;
}
/**
* Returns only stores in the given country.
* Countries are specified using ISO 3166-1 alpha-2,
* DK for denmark, SE for Sweden etc.
*
* @param {string} country Country code to get stores for.
* @returns {StoresQuery}
*/
inCountry(country) {
this.set('country', country);
return this;
}
/**
* Executes the query.
*
* @returns {Traverser} A traverser to use for iteration.
*/
execute() {
return this.storesAPI.query(this.params);
}
}
const BASE_URL = '/v1/stores/';
/**
* Wraps the Salling Group Stores API.
*/
class StoresSDK {
/**
* Initializes a new Stores API wrapper.
* @param {Object} options A SallingGroupAPI instance.
*/
constructor(options) {
this.instance = createInstance({
...options,
'baseName': `Stores SDK v${pkg.version}`,
});
}
/**
* Get a specific store.
*
* @param storeID The ID of the store.
* @returns {Promise<Object|null>} Returns the store. If it cannot be found, then it returns null.
*/
async get(storeID) {
try {
const result = await this.instance.get(`${BASE_URL}${storeID}`);
return result.data;
} catch (error) {
if (error.statusCode === 404) {
return null;
}
throw error;
}
}
/**
* Query stores based on search parameters.
*
* @param {Object} params The search parameters.
* @returns {Traverser} A traverser to use for iteration over the stores.
*/
query(params = {}) {
return new Traverser(this.instance, BASE_URL, { params });
}
/**
* Gets all stores.
*
* @returns {Traverser} A traverser to use for iteration over the stores.
*/
getAll() {
return this.query();
}
/**
* Begins a store query. This supports chain calling. Execute the query by running .execute().
*
* @returns {StoresQuery} The query to be built. Execute with .execute().
*/
beginQuery() {
return new StoresQuery(this);
}
}
module.exports = StoresSDK;