Skip to content

Commit

Permalink
feat(core): add platform and os detection for user-agent Closes #29
Browse files Browse the repository at this point in the history
Closes #29
  • Loading branch information
Wajahat Iqbal authored Dec 22, 2021
1 parent eb87434 commit a3d0023
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@apimatic/core",
"author": "Wajahat Iqbal",
"version": "0.6.2",
"version": "0.7.0",
"license": "SEE LICENSE IN LICENSE.md",
"sideEffects": false,
"main": "lib/index.js",
Expand Down Expand Up @@ -61,6 +61,7 @@
"@apimatic/json-bigint": "^1.1.0",
"@apimatic/schema": "^0.6.0",
"axios": "^0.21.1",
"detect-browser": "^5.3.0",
"detect-node": "^2.0.4",
"form-data": "^3.0.0",
"lodash.flatmap": "^4.5.0",
Expand Down
39 changes: 39 additions & 0 deletions packages/core/src/apiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* This file was automatically generated by APIMATIC v2.0 ( https://apimatic.io ).
*/

import { detect } from 'detect-browser';
import warning from 'tiny-warning';

/**
Expand Down Expand Up @@ -71,3 +72,41 @@ export function deprecated(methodName: string, notice?: string): void {
warning(false, message);
}
}

/**
* Replace the templated placeholders in user-agent with the platform
* related information.
* @param userAgent User-agent value to be updated
* @returns Updated user-agent value
*/
export function updateUserAgent(
userAgent: string,
apiVersion?: string,
detail?: string
): string {
let updatedAgent = userAgent;
const result = detect();
if (result) {
updatedAgent = updatedAgent.replace('{engine}', result.name);
}
if (result?.version) {
updatedAgent = updatedAgent.replace('{engine-version}', result.version);
}
if (result?.os) {
updatedAgent = updatedAgent.replace('{os-info}', result.os);
}
if (typeof apiVersion !== 'undefined') {
updatedAgent = updatedAgent.replace('{api-version}', apiVersion);
}
if (typeof detail !== 'undefined') {
assertUserAgentDetail(detail);
updatedAgent = updatedAgent.replace('{detail}', encodeURIComponent(detail));
}
return updatedAgent;
}

function assertUserAgentDetail(detail: string) {
if (detail.length > 128) {
throw new Error('userAgentDetail length exceeds 128 characters limit');
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3766,6 +3766,11 @@ des.js@^1.0.0:
inherits "^2.0.1"
minimalistic-assert "^1.0.0"

detect-browser@^5.3.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/detect-browser/-/detect-browser-5.3.0.tgz#9705ef2bddf46072d0f7265a1fe300e36fe7ceca"
integrity sha512-53rsFbGdwMwlF7qvCt0ypLM5V5/Mbl0szB7GPN8y9NCcbknYOeVVXdrXEq+90IwAfrrzt6Hd+u2E2ntakICU8w==

detect-indent@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-5.0.0.tgz#3871cc0a6a002e8c3e5b3cf7f336264675f06b9d"
Expand Down

0 comments on commit a3d0023

Please sign in to comment.