-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IO-363] add Accept and ContentType headers
- Loading branch information
Showing
7 changed files
with
184 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 42 additions & 18 deletions
60
src/language/typescript/3.0/serializers/request-body-object.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,59 @@ | ||
import { serializeSchemaObject } from './schema-object'; | ||
import { getSerializedRefType, SerializedType } from '../../common/data/serialized-type'; | ||
import { | ||
getSerializedBlobType, | ||
getSerializedRefType, | ||
SerializedType, | ||
SERIALIZED_STRING_TYPE, | ||
} from '../../common/data/serialized-type'; | ||
import { Either, mapLeft } from 'fp-ts/lib/Either'; | ||
import { pipe } from 'fp-ts/lib/pipeable'; | ||
import { either, option } from 'fp-ts'; | ||
import { fromString, Ref } from '../../../../utils/ref'; | ||
import { RequestBodyObject } from '../../../../schema/3.0/request-body-object'; | ||
import { ReferenceObjectCodec, ReferenceObject } from '../../../../schema/3.0/reference-object'; | ||
import { SchemaObject } from '../../../../schema/3.0/schema-object'; | ||
import { getKeyMatchValue } from '../../common/utils'; | ||
import { getKeyMatchValue, getResponseTypeFromMediaType, XHRResponseType } from '../../common/utils'; | ||
import { MediaTypeObject } from '../../../../schema/3.0/media-type-object'; | ||
|
||
const requestMediaRegexp = /^(video|audio|image|application|text|multipart\/form-data)/; | ||
export const getRequestMedia = (content: Record<string, MediaTypeObject>) => | ||
getKeyMatchValue(content, requestMediaRegexp); | ||
|
||
export const serializeRequestBodyObject = (from: Ref, body: RequestBodyObject): Either<Error, SerializedType> => | ||
pipe( | ||
getSchema(body), | ||
either.chain(schema => | ||
ReferenceObjectCodec.is(schema) | ||
getRequestMedia(body.content), | ||
option.chain(({ key: mediaType, value: { schema } }) => | ||
pipe( | ||
schema, | ||
option.map(schema => ({ mediaType, schema })), | ||
), | ||
), | ||
either.fromOption(() => new Error('No schema found for ReqeustBodyObject')), | ||
either.chain(({ mediaType, schema }) => { | ||
const resType = getResponseTypeFromMediaType(mediaType); | ||
return serializeRequestSchema(resType, schema, from); | ||
}), | ||
); | ||
|
||
const serializeRequestSchema = ( | ||
responseType: XHRResponseType, | ||
schema: ReferenceObject | SchemaObject, | ||
from: Ref, | ||
): Either<Error, SerializedType> => { | ||
switch (responseType) { | ||
case 'json': | ||
return ReferenceObjectCodec.is(schema) | ||
? pipe( | ||
schema.$ref, | ||
fromString, | ||
fromString(schema.$ref), | ||
mapLeft( | ||
() => new Error(`Invalid MediaObject.content.$ref "${schema.$ref}" for RequestBodyObject`), | ||
), | ||
either.map(getSerializedRefType(from)), | ||
) | ||
: serializeSchemaObject(from)(schema), | ||
), | ||
); | ||
|
||
const requestMediaRegexp = /^(video|audio|image|application|text|multipart\/form-data)/; | ||
const getSchema = (requestBodyObject: RequestBodyObject): Either<Error, ReferenceObject | SchemaObject> => | ||
pipe( | ||
getKeyMatchValue(requestBodyObject.content, requestMediaRegexp), | ||
option.chain(media => media.schema), | ||
either.fromOption(() => new Error('No schema found for ReqeustBodyObject')), | ||
); | ||
: serializeSchemaObject(from)(schema); | ||
case 'text': | ||
return either.right(SERIALIZED_STRING_TYPE); | ||
case 'blob': | ||
return getSerializedBlobType(from); | ||
} | ||
}; |
48 changes: 39 additions & 9 deletions
48
src/language/typescript/3.0/serializers/response-object.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,57 @@ | ||
import { SerializedType, getSerializedRefType } from '../../common/data/serialized-type'; | ||
import { | ||
SerializedType, | ||
getSerializedRefType, | ||
SERIALIZED_STRING_TYPE, | ||
getSerializedBlobType, | ||
} from '../../common/data/serialized-type'; | ||
import { pipe } from 'fp-ts/lib/pipeable'; | ||
import { serializeSchemaObject } from './schema-object'; | ||
import { Either } from 'fp-ts/lib/Either'; | ||
import { fromString, Ref } from '../../../../utils/ref'; | ||
import { either, option } from 'fp-ts'; | ||
import { ResponseObject } from '../../../../schema/3.0/response-object'; | ||
import { Option } from 'fp-ts/lib/Option'; | ||
import { ReferenceObjectCodec } from '../../../../schema/3.0/reference-object'; | ||
import { getKeyMatchValue } from '../../common/utils'; | ||
import { ReferenceObject, ReferenceObjectCodec } from '../../../../schema/3.0/reference-object'; | ||
import { getKeyMatchValue, getResponseTypeFromMediaType, XHRResponseType } from '../../common/utils'; | ||
import { SchemaObject } from '../../../../schema/3.0/schema-object'; | ||
import { MediaTypeObject } from '../../../../schema/3.0/media-type-object'; | ||
|
||
const requestMediaRegexp = /^(video|audio|image|application|text)/; | ||
export const getResponseMedia = (content: Record<string, MediaTypeObject>) => | ||
getKeyMatchValue(content, requestMediaRegexp); | ||
|
||
export const serializeResponseObject = ( | ||
from: Ref, | ||
responseObject: ResponseObject, | ||
): Option<Either<Error, SerializedType>> => | ||
pipe( | ||
responseObject.content, | ||
option.chain(content => getKeyMatchValue(content, requestMediaRegexp)), | ||
option.chain(media => media.schema), | ||
option.map(schema => | ||
ReferenceObjectCodec.is(schema) | ||
? pipe(fromString(schema.$ref), either.map(getSerializedRefType(from))) | ||
: serializeSchemaObject(from)(schema), | ||
option.chain(content => getResponseMedia(content)), | ||
option.chain(({ key: mediaType, value: { schema } }) => | ||
pipe( | ||
schema, | ||
option.map(schema => ({ mediaType, schema })), | ||
), | ||
), | ||
option.map(({ mediaType, schema }) => { | ||
const resType = getResponseTypeFromMediaType(mediaType); | ||
return serializeResponseSchema(resType, schema, from); | ||
}), | ||
); | ||
|
||
const serializeResponseSchema = ( | ||
responseType: XHRResponseType, | ||
schema: ReferenceObject | SchemaObject, | ||
from: Ref, | ||
): Either<Error, SerializedType> => { | ||
switch (responseType) { | ||
case 'json': | ||
return ReferenceObjectCodec.is(schema) | ||
? pipe(fromString(schema.$ref), either.map(getSerializedRefType(from))) | ||
: serializeSchemaObject(from)(schema); | ||
case 'text': | ||
return either.right(SERIALIZED_STRING_TYPE); | ||
case 'blob': | ||
return getSerializedBlobType(from); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.