Skip to content

Commit

Permalink
Use latest common & the new fetcher API (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
z4kn4fein authored Apr 22, 2022
1 parent d181647 commit 183cefc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "configcat-js-ssr",
"version": "3.8.0",
"version": "3.9.0",
"description": "ConfigCat Feature Flags for Server Side Rendered apps like NuxtJS. Official ConfigCat SDK for Server Side Rendered to easily access feature flags.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -40,7 +40,7 @@
},
"dependencies": {
"axios": "^0.25.0",
"configcat-common": "^4.6.2"
"configcat-common": "^5.0.1"
},
"devDependencies": {
"@types/chai": "^4.2.4",
Expand Down
19 changes: 9 additions & 10 deletions src/ConfigFetcher.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
import { IConfigFetcher, ProjectConfig, OptionsBase } from "configcat-common";
import { IConfigFetcher, ProjectConfig, OptionsBase, FetchResult } from "configcat-common";
import axios, { AxiosRequestConfig } from 'axios';


export class HttpConfigFetcher implements IConfigFetcher {

fetchLogic(options: OptionsBase, lastProjectConfig: ProjectConfig, callback: (newProjectConfig: ProjectConfig) => void): void {
fetchLogic(options: OptionsBase, lastEtag: string, callback: (result: FetchResult) => void): void {

const axiosConfig: AxiosRequestConfig = {
method: 'get',
timeout: options.requestTimeoutMs,
url: options.getUrl(),
headers: {
'X-ConfigCat-UserAgent': `ConfigCat-JS-SSR/${options.clientVersion}`,
'If-None-Match': (lastProjectConfig && lastProjectConfig.HttpETag) ? lastProjectConfig.HttpETag : null
'If-None-Match': (lastEtag) ? lastEtag : null
}
};

axios(axiosConfig)
.then(response => {
const eTag = response.headers.etag as string;
if (response.status === 200) {
callback(new ProjectConfig(new Date().getTime(), JSON.stringify(response.data), eTag));
callback(FetchResult.success(JSON.stringify(response.data), eTag));
} else {
options.logger.error(`Failed to download feature flags & settings from ConfigCat. ${response.status} - ${response.statusText}`);
options.logger.info("Double-check your SDK Key on https://app.configcat.com/sdkkey");
callback(lastProjectConfig);
callback(FetchResult.error());
}
})
.catch(error => {
if (error.response) {
if (error.response.status === 304) {
const eTag = error.response.headers.etag as string;
callback(new ProjectConfig(new Date().getTime(), JSON.stringify(lastProjectConfig.ConfigJSON), eTag));
callback(FetchResult.notModified());
} else {
options.logger.error(`Failed to download feature flags & settings from ConfigCat. ${error.response.status} - ${error.response.statusText}`);
options.logger.info("Double-check your SDK Key on https://app.configcat.com/sdkkey");
callback(lastProjectConfig);
callback(FetchResult.error());
}
} else if (error.request) {
options.logger.error('The request to Configcat was made but no response was received');
callback(lastProjectConfig);
callback(FetchResult.error());
} else {
options.logger.error(`Something happened in setting up the request to ConfigCat: ${error.message}`);
callback(lastProjectConfig);
callback(FetchResult.error());
}
});
}
Expand Down

0 comments on commit 183cefc

Please sign in to comment.