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

feat: abstract from data structure in client #60

Merged
merged 6 commits into from
Oct 31, 2019
Merged
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
10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,30 @@
},
"dependencies": {
"@devexperts/utils": "^1.0.0-alpha.2",
"@types/del": "^3.0.1",
"@types/fs-extra": "^8.0.0",
"@types/js-yaml": "^3.11.2",
"@types/prettier": "^1.13.2",
"del": "^3.0.0",
"fs-extra": "^8.1.0",
"js-yaml": "^3.13.1",
"json-schema-ref-parser": "^7.1.1",
"prettier": "^1.15.3"
},
"devDependencies": {
"@devexperts/lint": "^1.0.0-alpha.2",
"@devexperts/remote-data-ts": "^2.0.0",
"@devexperts/rx-utils": "^1.0.0-alpha.2",
"@types/del": "^4.0.0",
"@types/jest": "^24.0.18",
"conventional-changelog-cli": "^2.0.21",
"del": "^5.1.0",
"eslint": "^6.5.1",
"fast-check": "^1.17.0",
"fp-ts": "^2.1.0",
"io-ts": "^2.0.1",
"io-ts-types": "^0.5.1",
"jest": "^24.9.0",
"nodemon": "^1.18.3",
"rxjs": "^6.3.3",
"ts-jest": "^24.1.0",
"ts-node": "^8.4.1",
"typescript": "^3.6.3"
},
"peerDependencies": {
"@devexperts/remote-data-ts": "^2.0.0",
"fp-ts": "^2.1.0",
"io-ts": "^2.0.1",
"io-ts-types": "^0.5.1"
Expand Down
48 changes: 0 additions & 48 deletions src/language/typescript/2.0-rx/serializers/paths-object.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ import {
SerializedParameter,
serializedParameter,
} from '../../common/data/serialized-parameter';
import { EMPTY_DEPENDENCIES, serializedDependency } from '../../common/data/serialized-dependency';
import {
EMPTY_DEPENDENCIES,
getSerializedKindDependency,
serializedDependency,
} from '../../common/data/serialized-dependency';
import { identity } from 'fp-ts/lib/function';
import { concatIf, concatIfL } from '../../../../utils/array';
import { unless, when } from '../../../../utils/string';
import { Context, getJSDoc, getURL, HTTPMethod } from '../../common/utils';
import { Context, getJSDoc, getURL, HTTPMethod, getKindValue } from '../../common/utils';
import { Either, isLeft, right } from 'fp-ts/lib/Either';
import { array, either, nonEmptyArray, option } from 'fp-ts';
import { combineEither, sequenceEither } from '@devexperts/utils/dist/adt/either.utils';
Expand Down Expand Up @@ -49,6 +53,7 @@ import { serializeSchemaObject } from './schema-object';
import { traverseArrayEither } from '../../../../utils/either';
import { PathItemObject } from '../../../../schema/2.0/path-item-object';
import { Eq, eqString, getStructEq } from 'fp-ts/lib/Eq';
import { Kind } from '../../../../utils/types';

interface Parameters {
readonly pathParameters: PathParameterObject[];
Expand Down Expand Up @@ -221,6 +226,7 @@ export const serializeOperationObject = combineReader(
from: Ref,
url: string,
method: HTTPMethod,
kind: Kind,
operation: OperationObject,
pathItem: PathItemObject,
): Either<Error, SerializedType> => {
Expand Down Expand Up @@ -283,7 +289,7 @@ export const serializeOperationObject = combineReader(

const type = `
${getJSDoc(array.compact([deprecated, operation.summary, paramsSummary]))}
readonly ${operationName}: (${argsType}) => LiveData<Error, ${serializedResponses.type}>;
readonly ${operationName}: (${argsType}) => ${getKindValue(kind, serializedResponses.type)};
`;

const serializedUrl = getURL(url, parameters.serializedPathParameters);
Expand All @@ -292,41 +298,30 @@ export const serializeOperationObject = combineReader(
${operationName}: (${argsName}) => {
${when(hasParameters, `const encoded = partial({ ${serializedParameters.io} }).encode(parameters);`)}

return e.apiClient
.request({
return e.httpClient.chain(
e.httpClient.request({
url: ${serializedUrl},
method: '${method}',
${when(hasQueryParameters, 'query: encoded.query,')}
${when(hasBodyParameters, 'body: encoded.body,')}
})
.pipe(
map(data =>
pipe(
data,
chain(value =>
fromEither<Error, ${serializedResponses.type}>(
pipe(
${serializedResponses.io}.decode(value),
mapLeft(ResponseValidationError.create),
),
),
),
),
}),
value =>
pipe(
${serializedResponses.io}.decode(value),
either.mapLeft(ResponseValidationError.create),
either.fold(error => e.httpClient.throwError(error), decoded => e.httpClient.of(decoded)),
),
);
);
},
`;

const dependencies = concatIfL(
hasParameters,
[
serializedDependency('map', 'rxjs/operators'),
serializedDependency('fromEither', '@devexperts/remote-data-ts'),
serializedDependency('chain', '@devexperts/remote-data-ts'),
serializedDependency('ResponseValidationError', getRelativePath(from, clientRef)),
serializedDependency('LiveData', '@devexperts/rx-utils/dist/rd/live-data.utils'),
serializedDependency('pipe', 'fp-ts/lib/pipeable'),
serializedDependency('mapLeft', 'fp-ts/lib/Either'),
serializedDependency('either', 'fp-ts'),
getSerializedKindDependency(kind),
...flatten(parameters.serializedPathParameters.map(parameter => parameter.dependencies)),
...serializedResponses.dependencies,
...serializedParameters.dependencies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { pipe } from 'fp-ts/lib/pipeable';
import { isSome, map, Option } from 'fp-ts/lib/Option';
import { serializeOperationObject } from './operation-object';
import { array, flatten } from 'fp-ts/lib/Array';
import { Dictionary } from '../../../../utils/types';
import { Dictionary, Kind } from '../../../../utils/types';
import { file, File } from '../../../../utils/fs';
import { serializedDependency, serializeDependencies } from '../../common/data/serialized-dependency';
import { decapitalize } from '@devexperts/utils/dist/string';
Expand All @@ -21,43 +21,43 @@ import { ask } from 'fp-ts/lib/Reader';
import { Context } from '../../common/utils';

const serializePath = combineReader(ask<Context>(), serializeOperationObject, (e, serializeOperationObject) => {
const run = (from: Ref, url: string, item: PathItemObject): Either<Error, SerializedType> => {
const run = (from: Ref, url: string, kind: Kind, item: PathItemObject): Either<Error, SerializedType> => {
if (isSome(item.$ref)) {
const $ref = item.$ref.value;
return pipe(
e.resolveRef({ $ref }),
PathItemObjectCodec.decode,
either.mapLeft(() => new Error(`Unable to resolve PathItem $ref: "${$ref}"`)),
either.chain(resolved => run(from, url, resolved)),
either.chain(resolved => run(from, url, kind, resolved)),
);
} else {
const get = pipe(
item.get,
map(operation => serializeOperationObject(from, url, 'GET', operation, item)),
map(operation => serializeOperationObject(from, url, 'GET', kind, operation, item)),
);
const put = pipe(
item.put,
map(operation => serializeOperationObject(from, url, 'PUT', operation, item)),
map(operation => serializeOperationObject(from, url, 'PUT', kind, operation, item)),
);
const post = pipe(
item.post,
map(operation => serializeOperationObject(from, url, 'POST', operation, item)),
map(operation => serializeOperationObject(from, url, 'POST', kind, operation, item)),
);
const remove = pipe(
item.delete,
map(operation => serializeOperationObject(from, url, 'DELETE', operation, item)),
map(operation => serializeOperationObject(from, url, 'DELETE', kind, operation, item)),
);
const options = pipe(
item.options,
map(operation => serializeOperationObject(from, url, 'OPTIONS', operation, item)),
map(operation => serializeOperationObject(from, url, 'OPTIONS', kind, operation, item)),
);
const head = pipe(
item.head,
map(operation => serializeOperationObject(from, url, 'HEAD', operation, item)),
map(operation => serializeOperationObject(from, url, 'HEAD', kind, operation, item)),
);
const patch = pipe(
item.patch,
map(operation => serializeOperationObject(from, url, 'PATCH', operation, item)),
map(operation => serializeOperationObject(from, url, 'PATCH', kind, operation, item)),
);
const operations = [get, put, post, remove, options, head, patch];
return pipe(
Expand All @@ -74,34 +74,72 @@ const serializePath = combineReader(ask<Context>(), serializeOperationObject, (e
export const serializePathGroup = combineReader(
serializePath,
serializePath => (from: Ref, name: string, group: Dictionary<PathItemObject>): Either<Error, File> => {
const serialized = pipe(
const serializedHKT = pipe(
group,
record.collect((url, item) => serializePath(from, url, item)),
record.collect((url, item) => serializePath(from, url, 'HKT', item)),
sequenceEither,
either.map(foldSerializedTypes),
);

return combineEither(serialized, clientRef, (serialized, clientRef) => {
const dependencies = serializeDependencies([
...serialized.dependencies,
serializedDependency('asks', 'fp-ts/lib/Reader'),
serializedDependency('APIClient', getRelativePath(from, clientRef)),
]);
return file(
`${from.name}.ts`,
`
const serializedKind = pipe(
group,
record.collect((url, item) => serializePath(from, url, '*', item)),
sequenceEither,
either.map(foldSerializedTypes),
);

const serializedKind2 = pipe(
group,
record.collect((url, item) => serializePath(from, url, '* -> *', item)),
sequenceEither,
either.map(foldSerializedTypes),
);

return combineEither(
serializedHKT,
serializedKind,
serializedKind2,
clientRef,
(serializedHKT, serializedKind, serializedKind2, clientRef) => {
const dependencies = serializeDependencies([
...serializedHKT.dependencies,
...serializedKind.dependencies,
...serializedKind2.dependencies,
serializedDependency('HTTPClient', getRelativePath(from, clientRef)),
serializedDependency('HTTPClient1', getRelativePath(from, clientRef)),
serializedDependency('HTTPClient2', getRelativePath(from, clientRef)),
serializedDependency('URIS', 'fp-ts/lib/HKT'),
serializedDependency('URIS2', 'fp-ts/lib/HKT'),
]);
return file(
`${from.name}.ts`,
`
${dependencies}

export interface ${from.name} {
${serialized.type}
export interface ${from.name}<F> {
${serializedHKT.type}
}

export interface ${from.name}1<F extends URIS> {
${serializedKind.type}
}

export interface ${from.name}2<F extends URIS2> {
${serializedKind2.type}
}

export const ${decapitalize(from.name)} = asks((e: { apiClient: APIClient }): ${from.name} => ({
${serialized.io}
}));
export function ${decapitalize(from.name)}<F extends URIS2>(e: { httpClient: HTTPClient2<F> }): ${from.name}2<F>
export function ${decapitalize(from.name)}<F extends URIS>(e: { httpClient: HTTPClient1<F> }): ${from.name}1<F>
export function ${decapitalize(from.name)}<F>(e: { httpClient: HTTPClient<F> }): ${from.name}<F>;
export function ${decapitalize(from.name)}<F>(e: { httpClient: HTTPClient<F> }): ${from.name}<F> {
return {
${serializedHKT.io}
}
}
`,
);
});
);
},
);
},
);

Expand Down
Loading