Skip to content

Commit

Permalink
Switch to imported json, fix typescript issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleyfrieze authored and hfranklin committed May 14, 2024
1 parent 7a48301 commit 49a0eca
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 86 deletions.
12 changes: 2 additions & 10 deletions src/api/BaseApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import axios, {
} from 'axios';
import axiosRetry from 'axios-retry';
import { randomUUID } from 'crypto';
import fs from 'fs';
import HttpsProxyAgent from 'https-proxy-agent';
import path from 'path';
import { fileURLToPath } from 'url';

import _curlirize from '../ext/axios-curlirize/curlirize';
import StateImpl, { State } from '../shared/State';
import { getUserAgent } from '../shared/Version';
import { curlirizeMessage, printMessage } from '../utils/Console';
import { mergeDeep } from '../utils/JsonUtils';
import { setupPollyForFrodoLib } from '../utils/SetupPollyForFrodoLib';
Expand All @@ -22,12 +20,6 @@ if (process.env.FRODO_MOCK) {
setupPollyForFrodoLib({ state: StateImpl({}) });
}

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const pkg = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '../../package.json'), 'utf8')
);

axiosRetry(axios, {
retries: 3,
shouldResetTimeout: true,
Expand All @@ -43,7 +35,7 @@ const maxSockets = 100;
const maxFreeSockets = 10;
const freeSocketTimeout = 30000;

const userAgent = `${pkg.name}/${pkg.version}`;
const userAgent = getUserAgent();
const transactionId = `frodo-${randomUUID()}`;
let httpAgent, httpsAgent;

Expand Down
55 changes: 7 additions & 48 deletions src/ops/AdminOps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath, URL } from 'url';
import { URL } from 'url';
import util from 'util';
import { v4 as uuidv4 } from 'uuid';

Expand Down Expand Up @@ -39,6 +37,12 @@ import {
import { accessTokenRfc7523AuthZGrant } from './OAuth2OidcOps';
import { updateOAuth2TrustedJwtIssuer } from './OAuth2TrustedJwtIssuerOps';
import { getRealmManagedOrganization } from './OrganizationOps';
import templateIpAddresses from './templates/autoaccess/IPAddresses.json';
import templateUserAgents from './templates/autoaccess/UserAgents.json';
import templateUsernames from './templates/autoaccess/Usernames.json';
import GENERIC_EXTENSION_ATTRIBUTES from './templates/cloud/GenericExtensionAttributesTemplate.json';
import OAUTH2_CLIENT from './templates/OAuth2ClientTemplate.json';
import OAUTH2_ISSUER from './templates/OAuth2TrustedJwtIssuerTemplate.json';

export type Admin = {
generateRfc7523AuthZGrantArtefacts(
Expand Down Expand Up @@ -337,30 +341,6 @@ export default (state: State): Admin => {
};
};

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const OAUTH2_CLIENT: OAuth2ClientSkeleton = JSON.parse(
fs.readFileSync(
path.resolve(__dirname, './templates/OAuth2ClientTemplate.json'),
'utf8'
)
);
const OAUTH2_ISSUER: OAuth2TrustedJwtIssuerSkeleton = JSON.parse(
fs.readFileSync(
path.resolve(__dirname, './templates/OAuth2TrustedJwtIssuerTemplate.json'),
'utf8'
)
);
const GENERIC_EXTENSION_ATTRIBUTES = JSON.parse(
fs.readFileSync(
path.resolve(
__dirname,
'./templates/cloud/GenericExtensionAttributesTemplate.json'
),
'utf8'
)
);

const protectedClients = ['ui', 'idm-provisioning'];
const protectedSubjects = ['amadmin', 'autoid-resource-server'];

Expand Down Expand Up @@ -1573,27 +1553,6 @@ export async function repairOrgModel({
}
}

const templateUsernames: string[] = JSON.parse(
fs.readFileSync(
path.resolve(__dirname, './templates/autoaccess/Usernames.json'),
'utf8'
)
);

const templateUserAgents: string[] = JSON.parse(
fs.readFileSync(
path.resolve(__dirname, './templates/autoaccess/UserAgents.json'),
'utf8'
)
);

const templateIpAddresses: string[] = JSON.parse(
fs.readFileSync(
path.resolve(__dirname, './templates/autoaccess/IPAddresses.json'),
'utf8'
)
);

export function getUniqueValues(values: string[]): string[] {
return [...new Set(values)].filter((it) => it);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ops/PolicyOps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { type PolicyCondition, type PolicySkeleton } from '../api/PoliciesApi';
import { type PolicySetSkeleton } from '../api/PolicySetApi';
import { type ScriptSkeleton } from '../api/ScriptApi';
import { cloneDeep } from '../utils/JsonUtils';
import { PolicyExportInterface } from '../../types/ops/PolicyOps';
import { PolicyExportInterface } from './PolicyOps';

const ctx = autoSetupPolly();

Expand Down
2 changes: 1 addition & 1 deletion src/ops/PolicySetOps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import * as PolicySetOps from './PolicySetOps';
import { autoSetupPolly, filterRecording } from '../utils/AutoSetupPolly';
import { type PolicySetSkeleton } from '../api/PolicySetApi';
import { cloneDeep } from '../utils/JsonUtils';
import { PolicySetExportInterface } from '../../types/ops/PolicySetOps';
import { PolicySetExportInterface } from './PolicySetOps';

const ctx = autoSetupPolly();

Expand Down
4 changes: 2 additions & 2 deletions src/ops/ScriptOps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
* Note: FRODO_DEBUG=1 is optional and enables debug logging for some output
* in case things don't function as expected
*/
import { FrodoError, state } from '../index';
import { state } from '../index';
import * as ScriptOps from './ScriptOps';
import { autoSetupPolly, filterRecording } from '../utils/AutoSetupPolly';
import { type ScriptSkeleton } from '../../types/api/ScriptApi';
import { ScriptSkeleton } from '../api/ScriptApi';

const ctx = autoSetupPolly();

Expand Down
13 changes: 2 additions & 11 deletions src/ops/VersionUtils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

import { generateReleaseApi } from '../api/BaseApi';
import { State } from '../shared/State';
import { getVersionFromPackage } from '../shared/Version';

export type Version = {
getVersion(): string;
Expand All @@ -27,15 +24,9 @@ export default (state: State): Version => {
};
};

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const pkg = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '../../package.json'), 'utf8')
);

export function getVersion({ state }: { state: State }) {
// must initialize state to avoid library initialization issues
if (state) return `${pkg.version}`;
if (state) return getVersionFromPackage();
}

export async function getAllVersions({
Expand Down
13 changes: 2 additions & 11 deletions src/shared/State.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/* eslint-disable no-console */
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

import { FeatureInterface } from '../api/cloud/FeatureApi';
import { UserSessionMetaType } from '../ops/AuthenticateOps';
import { FrodoError } from '../ops/FrodoError';
Expand All @@ -13,12 +9,7 @@ import {
ProgressIndicatorType,
} from '../utils/Console';
import { cloneDeep } from '../utils/JsonUtils';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const pkg = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '../../package.json'), 'utf8')
);
import { getPackageVersion } from './Version';

export type State = {
/**
Expand Down Expand Up @@ -312,7 +303,7 @@ export default (initialState: StateInterface): State => {
state.frodoVersion = version;
},
getFrodoVersion() {
return state.frodoVersion || `v${pkg.version} [${process.version}]`;
return state.frodoVersion || getPackageVersion();
},

setConnectionProfilesPath(path: string) {
Expand Down
7 changes: 7 additions & 0 deletions src/shared/Version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getUserAgent } from './Version';

describe('Versions', () => {
test('user agent is compiled from package', () => {
expect(getUserAgent()).toMatch(/@rockcarver\/frodo-lib\/[0-9.-]+/);
});
});
7 changes: 7 additions & 0 deletions src/shared/Version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pkg from '../../package.json';

export const getUserAgent = () => `${pkg.name}/${pkg.version}`;

export const getPackageVersion = () => `v${pkg.version} [${process.version}]`;

export const getVersionFromPackage = () => pkg.version;
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "resolveJsonModule": true, /* Enable importing .json files. */
"resolveJsonModule": true, /* Enable importing .json files. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
Expand Down Expand Up @@ -101,7 +101,7 @@
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"files": ["src/index.ts"],
"include": ["src/**/*.ts"],
"include": ["src/**/*.ts","package.json"],
"exclude": ["src/**/*.test.ts", "src/**/*.test_.ts"],
"typedocOptions": {
"name": "Frodo Library",
Expand Down

0 comments on commit 49a0eca

Please sign in to comment.