Skip to content

Commit

Permalink
chore(synchronizer): reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
f1ames committed Nov 16, 2023
1 parent 107f9b1 commit 7c6a41b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 40 deletions.
34 changes: 20 additions & 14 deletions packages/synchronizer/src/__tests__/apiHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,23 @@ describe('ApiHandler Tests', () => {

describe('Api Url', () => {
it('uses default Api Url by default', () => {
assert.equal('https://api.monokle.com', (new ApiHandler()).apiUrl);
assert.equal('https://api.monokle.com', new ApiHandler().apiUrl);
});

it('uses default Api Url when falsy value passed', () => {
assert.equal('https://api.monokle.com', (new ApiHandler('')).apiUrl);
assert.equal('https://api.monokle.com', (new ApiHandler(false as any)).apiUrl);
assert.equal('https://api.monokle.com', (new ApiHandler(null as any)).apiUrl);
assert.equal('https://api.monokle.com', (new ApiHandler(undefined as any)).apiUrl);
assert.equal('https://api.monokle.com', (new ApiHandler(0 as any)).apiUrl);
assert.equal('https://api.monokle.com', (new ApiHandler([] as any)).apiUrl);
assert.equal('https://api.monokle.com', new ApiHandler('').apiUrl);
assert.equal('https://api.monokle.com', new ApiHandler(false as any).apiUrl);
assert.equal('https://api.monokle.com', new ApiHandler(null as any).apiUrl);
assert.equal('https://api.monokle.com', new ApiHandler(undefined as any).apiUrl);
assert.equal('https://api.monokle.com', new ApiHandler(0 as any).apiUrl);
assert.equal('https://api.monokle.com', new ApiHandler([] as any).apiUrl);
});

it('uses passed Api Url', () => {
assert.equal('https://dev.api.monokle.com', (new ApiHandler('https://dev.api.monokle.com')).apiUrl);
assert.equal('https://api.monokle.io', (new ApiHandler('https://api.monokle.io')).apiUrl);
assert.equal('http://localhost:5000', (new ApiHandler('http://localhost:5000')).apiUrl);
assert.equal('http://localhost', (new ApiHandler('http://localhost:80')).apiUrl);
assert.equal('https://dev.api.monokle.com', new ApiHandler('https://dev.api.monokle.com').apiUrl);
assert.equal('https://api.monokle.io', new ApiHandler('https://api.monokle.io').apiUrl);
assert.equal('http://localhost:5000', new ApiHandler('http://localhost:5000').apiUrl);
assert.equal('http://localhost', new ApiHandler('http://localhost:80').apiUrl);
});

it('uses passed origin data #1', () => {
Expand All @@ -87,9 +87,15 @@ describe('ApiHandler Tests', () => {
});

it('generates correct deep links', () => {
assert.equal('https://app.monokle.com/projects', (new ApiHandler('')).generateDeepLink('projects'));
assert.equal('https://app.staging.monokle.com/projects', (new ApiHandler('https://api.staging.monokle.com')).generateDeepLink('projects'));
assert.equal('http://localhost:5000/projects', (new ApiHandler('http://localhost:5000')).generateDeepLink('projects'));
assert.equal('https://app.monokle.com/projects', new ApiHandler('').generateDeepLink('projects'));
assert.equal(
'https://app.staging.monokle.com/projects',
new ApiHandler('https://api.staging.monokle.com').generateDeepLink('projects')
);
assert.equal(
'http://localhost:5000/projects',
new ApiHandler('http://localhost:5000').generateDeepLink('projects')
);

const apiHandler = new ApiHandler({
origin: 'https://custom.domain.io',
Expand Down
2 changes: 1 addition & 1 deletion packages/synchronizer/src/__tests__/fetcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sinon from 'sinon';
import {assert} from 'chai';
import express from 'express';
import {createDefaultMonokleFetcher} from '../createDefaultMonokleFetcher.js';
import { Fetcher } from '../utils/fetcher.js';
import {Fetcher} from '../utils/fetcher.js';

const TEST_QUERY = `
query getCluster($id: ID) {
Expand Down
24 changes: 16 additions & 8 deletions packages/synchronizer/src/handlers/apiHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fetch from 'node-fetch';
import {SuppressionStatus} from '@monokle/types';
import {DEFAULT_API_URL} from '../constants.js';
import type {TokenInfo} from './storageHandlerAuth.js';
import { OriginConfig } from '../utils/fetcher.js';
import {OriginConfig} from '../utils/fetcher.js';

const getUserQuery = `
query getUser {
Expand Down Expand Up @@ -170,10 +170,10 @@ export type ApiRepoIdData = {
data: {
getProject: {
repository: {
id: string
}
}
}
id: string;
};
};
};
};

export class ApiHandler {
Expand All @@ -184,10 +184,13 @@ export class ApiHandler {
constructor(_apiUrl: string);
constructor(_originConfig: OriginConfig);
constructor(_apiUrlOrOriginConfig: string | OriginConfig = DEFAULT_API_URL) {

if (typeof _apiUrlOrOriginConfig === 'string') {
this._apiUrl = _apiUrlOrOriginConfig;
} else if (_apiUrlOrOriginConfig !== null && typeof _apiUrlOrOriginConfig === 'object' && !Array.isArray(_apiUrlOrOriginConfig)) {
} else if (
_apiUrlOrOriginConfig !== null &&
typeof _apiUrlOrOriginConfig === 'object' &&
!Array.isArray(_apiUrlOrOriginConfig)
) {
this._originConfig = _apiUrlOrOriginConfig;
this._apiUrl = _apiUrlOrOriginConfig.apiOrigin;
} else {
Expand Down Expand Up @@ -219,7 +222,12 @@ export class ApiHandler {
return this.queryApi(getSuppressionsQuery, tokenInfo, {repositoryId});
}

async getRepoId(projectSlug: string, repoOwner: string, repoName: string, tokenInfo: TokenInfo): Promise<ApiRepoIdData | undefined> {
async getRepoId(
projectSlug: string,
repoOwner: string,
repoName: string,
tokenInfo: TokenInfo
): Promise<ApiRepoIdData | undefined> {
return this.queryApi(getRepoIdQuery, tokenInfo, {projectSlug, repoOwner, repoName});
}

Expand Down
15 changes: 9 additions & 6 deletions packages/synchronizer/src/utils/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ export class Fetcher extends EventEmitter {
const response = await fetch(configUrl);
const responseText = await response.text();

const values = Array.from(responseText.matchAll(/([A-Z_]+)\s*:\s*"(.*?)"/gm)).reduce((acc: Record<string, string>, match) => {
if (match[1] && match[2]) {
acc[match[1]] = match[2];
}
return acc;
}, {});
const values = Array.from(responseText.matchAll(/([A-Z_]+)\s*:\s*"(.*?)"/gm)).reduce(
(acc: Record<string, string>, match) => {
if (match[1] && match[2]) {
acc[match[1]] = match[2];
}
return acc;
},
{}
);

if (values) {
values.origin = normalizeUrl(origin);
Expand Down
38 changes: 27 additions & 11 deletions packages/synchronizer/src/utils/synchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class Synchronizer extends EventEmitter {

const projectSlugFromInput = this.getProjectSlug(rootPathOrRepoDataOrProjectData);
const freshProjectInfo = projectSlugFromInput
? await this.getProject({ slug: projectSlugFromInput }, tokenInfo)
? await this.getProject({slug: projectSlugFromInput}, tokenInfo)
: await this.getMatchingProject(inputData as RepoRemoteInputData, tokenInfo);

return !freshProjectInfo
Expand All @@ -92,7 +92,11 @@ export class Synchronizer extends EventEmitter {
}

async getPolicy(rootPath: string, forceRefetch?: boolean, tokenInfo?: TokenInfo): Promise<PolicyData>;
async getPolicy(rootPathWithProject: RepoPathInputData, forceRefetch?: boolean, tokenInfo?: TokenInfo): Promise<PolicyData>;
async getPolicy(
rootPathWithProject: RepoPathInputData,
forceRefetch?: boolean,
tokenInfo?: TokenInfo
): Promise<PolicyData>;
async getPolicy(repoData: RepoRemoteInputData, forceRefetch?: boolean, tokenInfo?: TokenInfo): Promise<PolicyData>;
async getPolicy(projectData: ProjectInputData, forceRefetch?: boolean, tokenInfo?: TokenInfo): Promise<PolicyData>;
async getPolicy(
Expand Down Expand Up @@ -150,7 +154,7 @@ export class Synchronizer extends EventEmitter {

const projectSlugFromInput = this.getProjectSlug(rootPathOrRepoDataOrProjectData);
if (projectSlugFromInput) {
this._pullPromise = this.fetchPolicyForProject({ slug: projectSlugFromInput }, tokenInfo);
this._pullPromise = this.fetchPolicyForProject({slug: projectSlugFromInput}, tokenInfo);
return this._pullPromise;
}

Expand Down Expand Up @@ -242,10 +246,17 @@ export class Synchronizer extends EventEmitter {

private async getRepoId(repoData: RepoRemoteInputData, tokenInfo: TokenInfo) {
if (repoData.ownerProjectSlug) {
const repoIdData = await this._apiHandler.getRepoId(repoData.ownerProjectSlug, repoData.owner, repoData.name, tokenInfo);
const repoIdData = await this._apiHandler.getRepoId(
repoData.ownerProjectSlug,
repoData.owner,
repoData.name,
tokenInfo
);

if (!repoIdData?.data?.getProject?.repository?.id) {
throw new Error(`The '${repoData.owner}/${repoData.name}' repository does not belong to a '${repoData.ownerProjectSlug}' project.`);
throw new Error(
`The '${repoData.owner}/${repoData.name}' repository does not belong to a '${repoData.ownerProjectSlug}' project.`
);
}

return repoIdData.data.getProject.repository.id;
Expand Down Expand Up @@ -325,7 +336,10 @@ export class Synchronizer extends EventEmitter {
}
}

private async getMatchingProject(repoData: RepoRemoteInputData, tokenInfo: TokenInfo): Promise<ApiUserProject | null> {
private async getMatchingProject(
repoData: RepoRemoteInputData,
tokenInfo: TokenInfo
): Promise<ApiUserProject | null> {
const userData = await this._apiHandler.getUser(tokenInfo);
if (!userData?.data?.me) {
throw new Error('Cannot fetch user data, make sure you are authenticated and have internet access.');
Expand Down Expand Up @@ -421,7 +435,9 @@ export class Synchronizer extends EventEmitter {
return `${prefix}-${repoData.provider}-${repoData.owner}-${repoData.name}`;
}

private async getRepoOrProjectData(inputData: string | RepoPathInputData | RepoRemoteInputData | ProjectInputData): Promise<ProjectInputData | RepoRemoteInputData> {
private async getRepoOrProjectData(
inputData: string | RepoPathInputData | RepoRemoteInputData | ProjectInputData
): Promise<ProjectInputData | RepoRemoteInputData> {
if (this.isProjectData(inputData)) {
return inputData as ProjectInputData;
}
Expand All @@ -434,7 +450,7 @@ export class Synchronizer extends EventEmitter {
};
}

return typeof inputData === 'string' ? await this.getRootGitData(inputData) : inputData as RepoRemoteData;
return typeof inputData === 'string' ? await this.getRootGitData(inputData) : (inputData as RepoRemoteData);
}

private isProjectData(projectData: any) {
Expand All @@ -446,8 +462,8 @@ export class Synchronizer extends EventEmitter {
}

private getProjectSlug(input: any) {
return this.isProjectData(input) || input.ownerProjectSlug?.length > 0 ?
input.slug ?? input.ownerProjectSlug :
undefined;
return this.isProjectData(input) || input.ownerProjectSlug?.length > 0
? input.slug ?? input.ownerProjectSlug
: undefined;
}
}

0 comments on commit 7c6a41b

Please sign in to comment.