Skip to content

Commit

Permalink
Set custom user agent for SDK
Browse files Browse the repository at this point in the history
In browser environment, the sdk user-agent string is appended to the browser's
own UA string.
  • Loading branch information
bladealslayer committed Apr 13, 2023
1 parent 641e594 commit d644111
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/developing-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ $ // git add, commit, push

#### Release a new version to NPM

1. Update version in `package.json`
1. Update version in `package.json` and `src/version.js`

1. Update CHANGELOG.md
- Move everything in Unreleased to the corresponding version section
Expand Down
4 changes: 3 additions & 1 deletion src/interceptors/endpoint_request.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import axios from 'axios';
import { sdkUserAgentString } from '../runtime';

// GET requests: `params` includes query params. `queryParams` will be ignored
// POST requests: `params` includes body params. `queryParams` includes URL query params
const doRequest = ({ params = {}, queryParams = {}, httpOpts }) => {
const { method = 'get' } = httpOpts;
const { method = 'get', headers } = httpOpts;

let data = null;
let query = null;
Expand All @@ -18,6 +19,7 @@ const doRequest = ({ params = {}, queryParams = {}, httpOpts }) => {

const req = {
...httpOpts,
headers: { ...headers, 'User-Agent': sdkUserAgentString },
method,
data,
params: query,
Expand Down
4 changes: 1 addition & 3 deletions src/multitenant_sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import AddMultitenantAuthHeader from './interceptors/add_multitenant_auth_header
import createSdkFnContextRunner from './sdk_context_runner';
import memoryStore from './memory_store';
import contextRunner from './context_runner';
import { isBrowser } from './runtime';

/* eslint-disable class-methods-use-this */

Expand Down Expand Up @@ -149,9 +150,6 @@ const validateSdkConfig = sdkConfig => {
throw new Error('baseUrl must be provided');
}

/* global window */
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';

if (isBrowser) {
throw new Error('Using the multitenant SDK in browser is not allowed.');
}
Expand Down
22 changes: 22 additions & 0 deletions src/runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sdkVersion from './version';

/* global window, navigator, process */
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
const navigatorUserAgent = typeof navigator !== 'undefined' ? navigator.userAgent : '';
const nodeVersion =
typeof process !== 'undefined' && typeof process.versions !== 'undefined'
? process.versions.node
: '';

// User-Agent string for the SDK
// For browsers, append to the browser's user agent string,
let userAgent = `sharetribe-flex-sdk-js/${sdkVersion}`;
if (isBrowser && navigatorUserAgent !== '') {
userAgent = `${navigatorUserAgent} ${userAgent}`;
} else if (nodeVersion !== '') {
userAgent = `${userAgent} (node/${nodeVersion})`;
}

const sdkUserAgentString = userAgent;

export { isBrowser, sdkUserAgentString };
5 changes: 2 additions & 3 deletions src/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import FormatHttpResponse from './interceptors/format_http_response';
import endpointRequest from './interceptors/endpoint_request';
import { createDefaultTokenStore } from './token_store';
import createSdkFnContextRunner from './sdk_context_runner';
import { isBrowser } from './runtime';

/* eslint-disable class-methods-use-this */

Expand Down Expand Up @@ -341,9 +342,7 @@ const validateSdkConfig = sdkConfig => {
throw new Error('assetCdnBaseUrl must be provided');
}

/* global window, console */
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';

/* global console */
if (isBrowser && sdkConfig.clientSecret && !sdkConfig.dangerouslyAllowClientSecretInBrowser) {
/* eslint-disable no-console */
console.warn(
Expand Down
3 changes: 3 additions & 0 deletions src/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Update this when updating package.json
const sdkVersion = '1.17.0';
export default sdkVersion;

0 comments on commit d644111

Please sign in to comment.