Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Razzmatazzz committed Jun 23, 2024
1 parent 507425f commit cf1aa45
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
7 changes: 3 additions & 4 deletions datasources/trader-inventory.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ class TraderInventoryAPI extends WorkerKV {
}

async getPricesForTrader(context, info, traderId) {
const { cache } = await this.getTraderCache(context, info, requestId);
const { cache } = await this.getTraderCache(context, info);
return cache[traderId] ?? [];
}

async getPricesForTraderLevel(context, info, traderId, level) {
const { cache } = await this.getTraderCache(context, info, requestId);
if (!cache[traderId]) return [];
return cache[traderId].filter(offer => {
const traderPrices = await this.getPricesForTrader(context, info, traderId);
return traderPrices.filter(offer => {
return offer.vendor.traderLevel === level;
});
}
Expand Down
58 changes: 27 additions & 31 deletions http/env-binding.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,35 @@ const accountId = '424ad63426a1ae47d559873f929eb9fc';
const productionNamespaceId = '2e6feba88a9e4097b6d2209191ed4ae5';
const devNameSpaceID = '17fd725f04984e408d4a70b37c817171';

const DATA_CACHE = {
get: async (kvName, format) => {
const namespaceId = process.env.ENVIRONMENT === 'production' ? productionNamespaceId : devNameSpaceID;
const url = `https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${kvName}`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.CLOUDFLARE_TOKEN}`,
},
});
if (response.status === 404) {
return null;
}
if (format === 'json') {
return response.json();
}
return response.text();
},
getWithMetadata: async (kvName, format) => {
return {
value: await DATA_CACHE.get(kvName, format),
};
},
};

export default function getEnv() {
return {
...process.env,
DATA_CACHE: {
get: async (kvName, format) => {
const namespaceId = process.env.ENVIRONMENT === 'production' ? productionNamespaceId : devNameSpaceID;
const url = `https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${kvName}`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.CLOUDFLARE_TOKEN}`,
},
});
if (format === 'json') {
return response.json();
}
return response.text();
},
getWithMetadata: async (kvName, format) => {
const namespaceId = process.env.ENVIRONMENT === 'production' ? productionNamespaceId : devNameSpaceID;
const url = `https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${kvName}`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.CLOUDFLARE_TOKEN}`,
},
});
return {
value: await response.text(),
};
},
},
DATA_CACHE,
}
};
11 changes: 6 additions & 5 deletions utils/worker-kv.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class WorkerKV {
return reject(new Error(`${metadata.compression} compression is not supported`));
}
const parsedValue = JSON.parse(responseValue);
if (parsedValue.errors && parsedValue.errors[0].code === 10009 && requestKv !== this.kvName) {
if (!parsedValue && requestKv !== this.kvName) {
console.warn(`${requestKv} data not found; falling back to ${this.kvName}`);
this.loading[gameMode] = false;
delete this.loadingPromises[gameMode][requestId];
Expand Down Expand Up @@ -120,12 +120,13 @@ class WorkerKV {
}
const lang = context.util.getLang(info, context);
const gameMode = this.getGameMode(context, info);
const cache = this.cache[gameMode];
const getTranslation = (k) => {
if (this.cache[gameMode]?.locale && this.cache[gameMode].locale[lang] && typeof this.cache[gameMode].locale[lang][k] !== 'undefined') {
return this.cache[gameMode].locale[lang][k];
if (cache?.locale[lang] && typeof cache.locale[lang][k] !== 'undefined') {
return cache.locale[lang][k];
}
if (this.cache[gameMode]?.locale && this.cache[gameMode].locale.en && typeof this.cache[gameMode].locale.en[k] !== 'undefined') {
return this.cache[gameMode].locale.en[k];
if (cache?.locale.en && typeof cache.locale.en[k] !== 'undefined') {
return cache.locale.en[k];
}
const errorMessage = `Missing translation for key ${k}`;
if (!context.errors.some(err => err.message === errorMessage)) {
Expand Down

0 comments on commit cf1aa45

Please sign in to comment.