|
| 1 | +import { getCollectionSeparator, serializeQueryParameterObject } from '../operation-object'; |
| 2 | +import { fromString } from '../../../../../utils/ref'; |
| 3 | +import { either } from 'fp-ts'; |
| 4 | +import { pipe } from 'fp-ts/lib/pipeable'; |
| 5 | +import { |
| 6 | + ArrayParameterObjectCollectionFormat, |
| 7 | + ArrayQueryParameterObjectCodec, |
| 8 | +} from '../../../../../schema/2.0/parameter-object'; |
| 9 | +import { combineEither, sequenceTEither } from '@devexperts/utils/dist/adt/either.utils'; |
| 10 | +import { reportIfFailed } from '../../../../../utils/io-ts'; |
| 11 | +import { serializedFragment } from '../../../common/data/serialized-fragment'; |
| 12 | +import { |
| 13 | + getSerializedArrayType, |
| 14 | + getSerializedIntegerType, |
| 15 | + getSerializedOptionalType, |
| 16 | +} from '../../../common/data/serialized-type'; |
| 17 | +import { utilsRef } from '../../../common/bundled/utils'; |
| 18 | +import { serializedDependency } from '../../../common/data/serialized-dependency'; |
| 19 | +import { serializeParameterObject } from '../parameter-object'; |
| 20 | + |
| 21 | +describe('Operation Object serialization', () => { |
| 22 | + it('serializeArrayQueryParameter', () => { |
| 23 | + const from = fromString('#/from'); |
| 24 | + const required = false; |
| 25 | + const target = 'parameters.query'; |
| 26 | + const collectionFormat: ArrayParameterObjectCollectionFormat = 'multi' as ArrayParameterObjectCollectionFormat; |
| 27 | + const name = 'queryParam'; |
| 28 | + const spec = ArrayQueryParameterObjectCodec.decode({ |
| 29 | + in: 'query', |
| 30 | + name, |
| 31 | + type: 'array', |
| 32 | + required, |
| 33 | + items: { |
| 34 | + type: 'integer', |
| 35 | + }, |
| 36 | + collectionFormat, |
| 37 | + }); |
| 38 | + |
| 39 | + const serialized = pipe( |
| 40 | + sequenceTEither(from, reportIfFailed(spec)), |
| 41 | + either.chain(([from, spec]) => |
| 42 | + pipe( |
| 43 | + serializeParameterObject(from, spec), |
| 44 | + either.map(serialized => getSerializedOptionalType(required, serialized)), |
| 45 | + either.chain(serialized => serializeQueryParameterObject(from, spec, serialized, target)), |
| 46 | + ), |
| 47 | + ), |
| 48 | + ); |
| 49 | + |
| 50 | + const expected = combineEither(from, utilsRef, (from, utilsRef) => { |
| 51 | + const type = pipe( |
| 52 | + getSerializedIntegerType(from, utilsRef), |
| 53 | + getSerializedArrayType(), |
| 54 | + serialized => getSerializedOptionalType(required, serialized), |
| 55 | + ); |
| 56 | + const encoded = `${type.io}.encode(${target}.${name})`; |
| 57 | + switch (collectionFormat) { |
| 58 | + case 'csv': |
| 59 | + case 'pipes': |
| 60 | + case 'ssv': |
| 61 | + case 'tsv': { |
| 62 | + const separator = getCollectionSeparator(collectionFormat); |
| 63 | + return serializedFragment( |
| 64 | + `${name}=\${${encoded}.join('${separator}')}`, |
| 65 | + type.dependencies, |
| 66 | + type.refs, |
| 67 | + ); |
| 68 | + } |
| 69 | + case 'multi': { |
| 70 | + const process = `.map(value => \`${name}=\${value}\`).join('&')`; |
| 71 | + if (required) { |
| 72 | + return serializedFragment(`\${${encoded}${process}}`, type.dependencies, type.refs); |
| 73 | + } else { |
| 74 | + return serializedFragment( |
| 75 | + `\${pipe(${encoded}, option.fromNullable, option.map(value => value${process}), option.getOrElse(() => ''))}`, |
| 76 | + [...type.dependencies, serializedDependency('option', 'fp-ts')], |
| 77 | + type.refs, |
| 78 | + ); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + }); |
| 83 | + expect(serialized).toEqual(expected); |
| 84 | + }); |
| 85 | +}); |
0 commit comments