Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "[tweek-client] add client error handling" #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/tweek-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tweek-client",
"version": "1.0.0",
"version": "1.0.0-rc7",
"description": "Tweek client for JavaScript",
"author": "Soluto",
"license": "MIT",
Expand Down
14 changes: 0 additions & 14 deletions js/tweek-client/src/TweekClient/KeyValuesError.ts

This file was deleted.

56 changes: 8 additions & 48 deletions js/tweek-client/src/TweekClient/TweekClient.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,9 @@
import { InputParams } from 'query-string';
import chunk from 'lodash.chunk';
import { deprecated, normalizeBaseUrl, optimizeInclude, toQueryString } from '../utils';
import { normalizeBaseUrl, optimizeInclude, toQueryString } from '../utils';
import { TweekInitConfig } from '../types';
import { FetchError } from '../FetchError';
import {
Context,
GetValuesConfig,
ITweekClient,
KeyValuesErrorHandler,
KeyValuesErrors,
TweekClientConfig,
} from './types';
import { KeyValuesError } from './KeyValuesError';

type TweekResult<T> = {
data: T;
errors: KeyValuesErrors;
};

function extractData<T>(
{ data, errors }: TweekResult<T>,
throwOnError: boolean | undefined,
onKeyValueError: KeyValuesErrorHandler | undefined,
) {
if (errors && Object.keys(errors).length > 0) {
if (onKeyValueError) {
Object.entries(errors as KeyValuesErrors).forEach(([k, e]) => onKeyValueError(k, e));
}
if (throwOnError) {
throw new KeyValuesError(errors, 'Tweek values had errors');
}
}
return data;
}
import { Context, GetValuesConfig, ITweekClient, TweekClientConfig } from './types';

export default class TweekClient implements ITweekClient {
config: TweekClientConfig;
Expand All @@ -54,37 +25,26 @@ export default class TweekClient implements ITweekClient {
..._config,
};

const { include, maxChunkSize = 100, throwOnError, onKeyValueError } = cfg;
const { include, maxChunkSize = 100 } = cfg;

if (!include) {
return this._fetchChunk<T>(path, cfg).then(res => extractData(res, throwOnError, onKeyValueError));
return this._fetchChunk(path, cfg);
}

const optimizedInclude = optimizeInclude(include);
const includeChunks = chunk(optimizedInclude, maxChunkSize);
const fetchConfigChunks = includeChunks.map(ic => ({ ...cfg, include: ic }));
const fetchPromises = fetchConfigChunks.map(cc => this._fetchChunk<T>(path, cc));
return Promise.all(fetchPromises).then(chunks => {
const res = chunks.reduce((res, ch) => ({
data: { ...res.data, ...ch.data },
errors: { ...res.errors, ...ch.errors },
}));
return extractData(res, throwOnError, onKeyValueError);
});
const fetchPromises = fetchConfigChunks.map(cc => this._fetchChunk(path, cc));
return <Promise<T>>Promise.all(fetchPromises).then(chunks => chunks.reduce((res, ch) => ({ ...res, ...ch }), {}));
}

@deprecated('getValues')
fetch<T>(path: string, config?: GetValuesConfig): Promise<T> {
return this.getValues(path, config);
}
fetch = this.getValues;

private _fetchChunk<T>(path: string, _config: TweekInitConfig & GetValuesConfig): Promise<TweekResult<T>> {
private _fetchChunk<T>(path: string, _config: TweekInitConfig & GetValuesConfig): Promise<T> {
const { flatten, baseServiceUrl, context, include, ignoreKeyTypes } = _config;

const queryParamsObject = this._contextToQueryParams(context);

queryParamsObject['$includeErrors'] = true;

if (flatten) {
queryParamsObject['$flatten'] = true;
}
Expand Down
19 changes: 0 additions & 19 deletions js/tweek-client/src/TweekClient/TweekClientWithFallback.ts

This file was deleted.

8 changes: 5 additions & 3 deletions js/tweek-client/src/TweekClient/createTweekClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { createFetchClient } from '../utils';
import { BaseCreateTweekClientConfig } from './types';
import { CreateClientConfig } from '../types';
import { Context } from './types';
import TweekClient from './TweekClient';

export type CreateTweekClientConfig = BaseCreateTweekClientConfig & {
baseServiceUrl: string;
export type CreateTweekClientConfig = CreateClientConfig & {
context?: Context;
useLegacyEndpoint?: boolean;
};

export function createTweekClient({
Expand Down
12 changes: 0 additions & 12 deletions js/tweek-client/src/TweekClient/createTweekClientWithFallback.ts

This file was deleted.

3 changes: 0 additions & 3 deletions js/tweek-client/src/TweekClient/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
export * from './types';
export { default as TweekClient } from './TweekClient';
export { default as TweekClientWithFallback } from './TweekClientWithFallback';
export * from './createTweekClient';
export * from './createTweekClientWithFallback';
export * from './KeyValuesError';
13 changes: 1 addition & 12 deletions js/tweek-client/src/TweekClient/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FetchClientConfig, IdentityContext, TweekInitConfig } from '../types';
import { IdentityContext, TweekInitConfig } from '../types';

export type Context = {
[identityType: string]: string | ({ id?: string } & IdentityContext);
Expand All @@ -8,28 +8,17 @@ type RequestConfig = {
include?: string[];
};

export type KeyValuesErrors = { [keyPath: string]: string };

export type KeyValuesErrorHandler = (keyPath: string, error: string) => void;

type ClientConfig = {
context?: Context;
flatten?: boolean;
ignoreKeyTypes?: boolean;
maxChunkSize?: number;
onKeyValueError?: KeyValuesErrorHandler;
throwOnError?: boolean;
};

export type GetValuesConfig = ClientConfig & RequestConfig;

export type TweekClientConfig = TweekInitConfig & ClientConfig;

export type BaseCreateTweekClientConfig = FetchClientConfig & {
context?: Context;
useLegacyEndpoint?: boolean;
};

export interface ITweekClient {
getValues<T>(path: string, config?: GetValuesConfig): Promise<T>;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { createFetchClient } from '../utils';
import { FetchClientConfig } from '../types';
import { CreateClientConfig } from '../types';
import TweekManagementClient from './TweekManagementClient';

export type CreateTweekManagementClientConfig = FetchClientConfig & {
baseServiceUrl: string;
};

export function createTweekManagementClient({
baseServiceUrl,
...fetchClientConfig
}: CreateTweekManagementClientConfig) {
export function createTweekManagementClient({ baseServiceUrl, ...fetchClientConfig }: CreateClientConfig) {
return new TweekManagementClient({
baseServiceUrl,
fetch: createFetchClient(fetchClientConfig),
Expand Down
4 changes: 4 additions & 0 deletions js/tweek-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ export type FetchClientConfig = (BearerAuthenticationOptions | ClientCredentials
requestTimeoutInMillis?: number;
onError?(response: Response): void;
};

export type CreateClientConfig = FetchClientConfig & {
baseServiceUrl: string;
};
18 changes: 0 additions & 18 deletions js/tweek-client/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,3 @@ export const toQueryString = (query: InputParams) => {
const queryString = qs.stringify(query);
return queryString ? `?${queryString}` : '';
};

export function deprecated(newMethod: string) {
let notified = false;
return function(target: Object, propertyKey: string, descriptor: PropertyDescriptor) {
const originalValue = descriptor.value;
descriptor.value = function() {
if (!notified) {
if (typeof process !== 'undefined' && process.env.NODE_ENV !== 'production') {
const name = target.constructor.name;
console.warn(`the ${name}.${propertyKey} method is deprecated, please use ${name}.${newMethod} instead`);
}
notified = true;
}

return originalValue.apply(this, arguments);
};
};
}
100 changes: 0 additions & 100 deletions js/tweek-client/test/TweekClient/errorHandling.spec.ts

This file was deleted.

12 changes: 6 additions & 6 deletions js/tweek-client/test/TweekClient/fetchChuncks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ describe('TweekClient fetchChunks', () => {
expectedUrl: `${defaultUrl}api/v2/values/_`,
stubCalls: [
{
requestUrl: 'http://test/api/v2/values/_?%24include=a1&%24include=a2&%24include=a3&%24includeErrors=true',
response: new Response('{"data": { "a1": 1, "a2": 2, "a3": 3 } }'),
requestUrl: 'http://test/api/v2/values/_?%24include=a1&%24include=a2&%24include=a3',
response: new Response('{ "a1": 1, "a2": 2, "a3": 3 }'),
},
{
requestUrl: 'http://test/api/v2/values/_?%24include=b1&%24include=b2&%24include=b3&%24includeErrors=true',
response: new Response('{ "data": { "b1": "a", "b2": "b", "b3": "c" } }'),
requestUrl: 'http://test/api/v2/values/_?%24include=b1&%24include=b2&%24include=b3',
response: new Response('{ "b1": "a", "b2": "b", "b3": "c" }'),
},
{
requestUrl: 'http://test/api/v2/values/_?%24include=c5&%24includeErrors=true',
response: new Response('{ "data": { "c5": true } }'),
requestUrl: 'http://test/api/v2/values/_?%24include=c5',
response: new Response('{ "c5": true }'),
},
],
config: {
Expand Down
Loading