venue-js 1.1.1 → 1.2.0-next.1

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/index.mjs CHANGED
@@ -79,7 +79,7 @@ var defaultFeatureQueryOptionsMap = {
79
79
  };
80
80
 
81
81
  // src/data/api/delivery-project.ts
82
- async function fetchFeature(projectId, apiKey, featureType, baseUrl = DEFAULT_BASE_URL) {
82
+ async function fetchDeliveryApi(projectId, apiKey, featureType, baseUrl = DEFAULT_BASE_URL) {
83
83
  switch (featureType) {
84
84
  case "label":
85
85
  case "element": {
@@ -113,14 +113,78 @@ async function fetchFeature(projectId, apiKey, featureType, baseUrl = DEFAULT_BA
113
113
  }
114
114
  }
115
115
  }
116
- var safeFetchFeature = async (projectId, apiKey, featureType, baseUrl = DEFAULT_BASE_URL) => {
116
+ async function fetchPreviewApi(projectId, previewToken, featureType, baseUrl = DEFAULT_BASE_URL) {
117
+ switch (featureType) {
118
+ case "label":
119
+ case "element": {
120
+ const pluralFeatureType = `${featureType}s`;
121
+ const res = await fetch(
122
+ `${baseUrl}/preview/projects/${projectId}/${pluralFeatureType}.geojson`,
123
+ {
124
+ headers: {
125
+ Authorization: `Bearer ${previewToken}`
126
+ }
127
+ }
128
+ );
129
+ if (res.status !== 200) return [];
130
+ const items = await res.json();
131
+ return items;
132
+ }
133
+ case "sponsored-content": {
134
+ const res = await fetch(
135
+ `${baseUrl}/preview/projects/${projectId}/sponsored-content.json`,
136
+ {
137
+ headers: {
138
+ Authorization: `Bearer ${previewToken}`
139
+ }
140
+ }
141
+ );
142
+ if (res.status !== 200) return [];
143
+ const jsonRes = await res.json();
144
+ const items = jsonRes.data;
145
+ return items.map((item) => ({
146
+ id: item.id,
147
+ ...item.attributes
148
+ }));
149
+ }
150
+ default: {
151
+ const res = await fetch(
152
+ `${baseUrl}/preview/projects/${projectId}/imdf/${featureType}.geojson`,
153
+ {
154
+ headers: {
155
+ Authorization: `Bearer ${previewToken}`
156
+ }
157
+ }
158
+ );
159
+ if (res.status !== 200) return [];
160
+ const collections = await res.json();
161
+ return collections.features;
162
+ }
163
+ }
164
+ }
165
+ var safeFetchFeature = async (featureType, params) => {
166
+ const mode = params.mode ?? "delivery";
167
+ const projectId = params.projectId;
168
+ const apiKey = params.apiKey;
169
+ const previewToken = params.previewToken;
170
+ const baseUrl = params.baseUrl ?? DEFAULT_BASE_URL;
117
171
  try {
118
- const result = await fetchFeature(
119
- projectId,
120
- apiKey,
121
- featureType,
122
- baseUrl
123
- );
172
+ let result = [];
173
+ if (mode === "delivery") {
174
+ result = await fetchDeliveryApi(
175
+ projectId,
176
+ apiKey,
177
+ featureType,
178
+ baseUrl
179
+ );
180
+ } else if (mode === "preview") {
181
+ result = await fetchPreviewApi(
182
+ projectId,
183
+ previewToken,
184
+ featureType,
185
+ baseUrl
186
+ );
187
+ }
124
188
  return result ?? [];
125
189
  } catch (e) {
126
190
  return Promise.resolve([]);
@@ -402,18 +466,22 @@ function matchFilters(item, filters) {
402
466
  var getDataClient = (options) => {
403
467
  const observers = /* @__PURE__ */ new Map();
404
468
  const queryClient = options.queryClient ?? new QueryClient();
405
- const { projectId, apiKey, baseUrl } = options;
469
+ const { mode = "delivery", projectId, apiKey, baseUrl, previewToken } = options;
406
470
  if (!projectId)
407
471
  throw new Error(
408
472
  "Cannot create VenueDataClient. Reason: `projectId` is missing"
409
473
  );
410
- if (!apiKey)
474
+ if (mode === "delivery" && !apiKey)
411
475
  throw new Error(
412
476
  "Cannot create VenueDataClient. Reason: `apiKey` is missing"
413
477
  );
478
+ if (mode === "preview" && !previewToken)
479
+ throw new Error(
480
+ "Cannot create VenueDataClient. Reason: `previewToken` is missing"
481
+ );
414
482
  const createDeliveryApiQueryOptions = (featureType) => ({
415
483
  queryKey: ["_deliveryapi", featureType],
416
- queryFn: () => safeFetchFeature(projectId, apiKey, featureType, baseUrl)
484
+ queryFn: () => safeFetchFeature(featureType, { mode, projectId, apiKey, previewToken, baseUrl })
417
485
  });
418
486
  const internalFilterByType = async (featureType) => {
419
487
  try {
@@ -5202,7 +5270,8 @@ export {
5202
5270
  createSpriteMaterialByLabelSymbol,
5203
5271
  createStyledUIMarkerElement,
5204
5272
  defaultFeatureQueryOptionsMap,
5205
- fetchFeature,
5273
+ fetchDeliveryApi,
5274
+ fetchPreviewApi,
5206
5275
  getBearingBetweenPoints,
5207
5276
  getCenterFromGeometry,
5208
5277
  getDataClient,