Skip to content

Commit e371f87

Browse files
skarimoci.datadog-api-spec
andauthored
Reduce and relax isNode check (#938)
* reduce isNode checks * update userAgent check as well * update logger * apply code review changes * pre-commit fixes Co-authored-by: ci.datadog-api-spec <[email protected]>
1 parent 5cb75d5 commit e371f87

File tree

6 files changed

+21
-11
lines changed

6 files changed

+21
-11
lines changed

.generator/src/generator/templates/configuration.j2

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { HttpLibrary, HttpConfiguration, RequestContext, ZstdCompressorCallback
22
import { IsomorphicFetchHttpLibrary as DefaultHttpLibrary } from "./http/isomorphic-fetch";
33
import { BaseServerConfiguration, server1, servers, operationServers } from "./servers";
44
import { configureAuthMethods, AuthMethods, AuthMethodsConfiguration } from "./auth";
5-
import { isNode } from "./util";
65

76
export interface Configuration {
87
readonly baseServer?: BaseServerConfiguration;
@@ -69,7 +68,7 @@ export interface ConfigurationParameters {
6968
* @param conf partial configuration
7069
*/
7170
export function createConfiguration(conf: ConfigurationParameters = {}): Configuration {
72-
if (isNode && process.env.DD_SITE) {
71+
if (typeof process !== "undefined" && process.env && process.env.DD_SITE) {
7372
const serverConf = server1.getConfiguration();
7473
server1.setVariables({"site": process.env.DD_SITE} as (typeof serverConf));
7574
for (const op in operationServers) {
@@ -80,7 +79,7 @@ export function createConfiguration(conf: ConfigurationParameters = {}): Configu
8079
const authMethods = conf.authMethods || {};
8180
{%- for name, schema in openapi.components.securitySchemes.items() %}
8281
{%- if schema.get("type") == "apiKey" and schema.get("in") == "header" %}
83-
if (!("{{ name }}" in authMethods) && isNode && process.env.{{ schema["x-env-name"] }}) {
82+
if (!("{{ name }}" in authMethods) && typeof process !== "undefined" && process.env && process.env.{{ schema["x-env-name"] }}) {
8483
authMethods["{{ name }}"] = process.env.{{ schema["x-env-name"] }};
8584
}
8685
{%- endif %}

.generator/src/generator/templates/util.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ export type AttributeTypeMap = {
1717

1818
export const isBrowser: boolean = typeof window !== "undefined" && typeof window.document !== "undefined";
1919

20-
export const isNode: boolean = typeof process !== "undefined" && process.release.name === 'node';
20+
export const isNode: boolean = typeof process !== "undefined" && process.release && process.release.name === 'node';

logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import log from "loglevel";
22

33
const logger = log.noConflict();
4-
logger.setLevel((typeof process !== "undefined" && process.release.name === 'node' && process.env.DEBUG) ? logger.levels.DEBUG : logger.levels.INFO);
4+
logger.setLevel((typeof process !== "undefined" && process.env && process.env.DEBUG) ? logger.levels.DEBUG : logger.levels.INFO);
55

66
export { logger };

packages/datadog-api-client-common/configuration.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
AuthMethods,
1717
AuthMethodsConfiguration,
1818
} from "./auth";
19-
import { isNode } from "./util";
2019

2120
export interface Configuration {
2221
readonly baseServer?: BaseServerConfiguration;
@@ -84,7 +83,7 @@ export interface ConfigurationParameters {
8483
export function createConfiguration(
8584
conf: ConfigurationParameters = {}
8685
): Configuration {
87-
if (isNode && process.env.DD_SITE) {
86+
if (typeof process !== "undefined" && process.env && process.env.DD_SITE) {
8887
const serverConf = server1.getConfiguration();
8988
server1.setVariables({ site: process.env.DD_SITE } as typeof serverConf);
9089
for (const op in operationServers) {
@@ -93,10 +92,20 @@ export function createConfiguration(
9392
}
9493

9594
const authMethods = conf.authMethods || {};
96-
if (!("apiKeyAuth" in authMethods) && isNode && process.env.DD_API_KEY) {
95+
if (
96+
!("apiKeyAuth" in authMethods) &&
97+
typeof process !== "undefined" &&
98+
process.env &&
99+
process.env.DD_API_KEY
100+
) {
97101
authMethods["apiKeyAuth"] = process.env.DD_API_KEY;
98102
}
99-
if (!("appKeyAuth" in authMethods) && isNode && process.env.DD_APP_KEY) {
103+
if (
104+
!("appKeyAuth" in authMethods) &&
105+
typeof process !== "undefined" &&
106+
process.env &&
107+
process.env.DD_APP_KEY
108+
) {
100109
authMethods["appKeyAuth"] = process.env.DD_APP_KEY;
101110
}
102111

packages/datadog-api-client-common/util.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ export const isBrowser: boolean =
1818
typeof window !== "undefined" && typeof window.document !== "undefined";
1919

2020
export const isNode: boolean =
21-
typeof process !== "undefined" && process.release.name === "node";
21+
typeof process !== "undefined" &&
22+
process.release &&
23+
process.release.name === "node";

userAgent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { version } from "./version";
22

33
export let userAgent: string;
4-
if (typeof process !== 'undefined' && process.release.name === 'node') {
4+
if (typeof process !== 'undefined' && process.release && process.release.name === 'node') {
55
userAgent = `datadog-api-client-typescript/${version} (node ${process.versions.node}; os ${process.platform}; arch ${process.arch})`
66
} else if (typeof window !== "undefined" && typeof window.document !== "undefined") {
77
// we don't set user-agent headers in browsers

0 commit comments

Comments
 (0)