-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* [APM] migrate to io-ts * Migrate remaining routes to io-ts * Infer response type for useFetcher() * Review feedback * Use createRangeType util * Extract & test runtime types * Simplify runtime types * Tests for createApi and callApmApi * Use more readable variable names in runtime types * Remove UIFilters query param for API endpoints where it is not supported * Fix issues w/ default parameters in create_api
- Loading branch information
1 parent
98993f5
commit 3f48d83
Showing
55 changed files
with
1,769 additions
and
1,463 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
x-pack/legacy/plugins/apm/common/runtime_types/date_as_string_rt/index.test.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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { dateAsStringRt } from './index'; | ||
|
||
describe('dateAsStringRt', () => { | ||
it('validates whether a string is a valid date', () => { | ||
expect(dateAsStringRt.decode(1566299881499).isLeft()).toBe(true); | ||
|
||
expect(dateAsStringRt.decode('2019-08-20T11:18:31.407Z').isRight()).toBe( | ||
true | ||
); | ||
}); | ||
|
||
it('returns the string it was given', () => { | ||
const either = dateAsStringRt.decode('2019-08-20T11:18:31.407Z'); | ||
|
||
expect(either.value).toBe('2019-08-20T11:18:31.407Z'); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
x-pack/legacy/plugins/apm/common/runtime_types/date_as_string_rt/index.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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import * as t from 'io-ts'; | ||
import { either } from 'fp-ts/lib/Either'; | ||
|
||
// Checks whether a string is a valid ISO timestamp, | ||
// but doesn't convert it into a Date object when decoding | ||
|
||
export const dateAsStringRt = new t.Type<string, string, unknown>( | ||
'DateAsString', | ||
t.string.is, | ||
(input, context) => | ||
either.chain(t.string.validate(input, context), str => { | ||
const date = new Date(str); | ||
return isNaN(date.getTime()) ? t.failure(input, context) : t.success(str); | ||
}), | ||
t.identity | ||
); |
43 changes: 43 additions & 0 deletions
43
x-pack/legacy/plugins/apm/common/runtime_types/json_rt/index.test.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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import * as t from 'io-ts'; | ||
import { jsonRt } from './index'; | ||
|
||
describe('jsonRt', () => { | ||
it('validates json', () => { | ||
expect(jsonRt.decode('{}').isRight()).toBe(true); | ||
expect(jsonRt.decode('[]').isRight()).toBe(true); | ||
expect(jsonRt.decode('true').isRight()).toBe(true); | ||
expect(jsonRt.decode({}).isLeft()).toBe(true); | ||
expect(jsonRt.decode('foo').isLeft()).toBe(true); | ||
}); | ||
|
||
it('returns parsed json when decoding', () => { | ||
expect(jsonRt.decode('{}').value).toEqual({}); | ||
expect(jsonRt.decode('[]').value).toEqual([]); | ||
expect(jsonRt.decode('true').value).toEqual(true); | ||
}); | ||
|
||
it('is pipable', () => { | ||
const piped = jsonRt.pipe(t.type({ foo: t.string })); | ||
|
||
const validInput = { foo: 'bar' }; | ||
const invalidInput = { foo: null }; | ||
|
||
const valid = piped.decode(JSON.stringify(validInput)); | ||
const invalid = piped.decode(JSON.stringify(invalidInput)); | ||
|
||
expect(valid.isRight()).toBe(true); | ||
expect(valid.value).toEqual(validInput); | ||
|
||
expect(invalid.isLeft()).toBe(true); | ||
}); | ||
|
||
it('returns strings when encoding', () => { | ||
expect(jsonRt.encode({})).toBe('{}'); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
x-pack/legacy/plugins/apm/common/runtime_types/json_rt/index.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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import * as t from 'io-ts'; | ||
import { either } from 'fp-ts/lib/Either'; | ||
|
||
export const jsonRt = new t.Type<any, string, unknown>( | ||
'JSON', | ||
t.any.is, | ||
(input, context) => | ||
either.chain(t.string.validate(input, context), str => { | ||
try { | ||
return t.success(JSON.parse(str)); | ||
} catch (e) { | ||
return t.failure(input, context); | ||
} | ||
}), | ||
a => JSON.stringify(a) | ||
); |
31 changes: 31 additions & 0 deletions
31
x-pack/legacy/plugins/apm/common/runtime_types/transaction_sample_rate_rt/index.test.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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { transactionSampleRateRt } from './index'; | ||
|
||
describe('transactionSampleRateRt', () => { | ||
it('accepts both strings and numbers as values', () => { | ||
expect(transactionSampleRateRt.decode('0.5').isRight()).toBe(true); | ||
expect(transactionSampleRateRt.decode(0.5).isRight()).toBe(true); | ||
}); | ||
|
||
it('checks if the number falls within 0, 1', () => { | ||
expect(transactionSampleRateRt.decode(0).isRight()).toBe(true); | ||
|
||
expect(transactionSampleRateRt.decode(0.5).isRight()).toBe(true); | ||
|
||
expect(transactionSampleRateRt.decode(-0.1).isRight()).toBe(false); | ||
expect(transactionSampleRateRt.decode(1.1).isRight()).toBe(false); | ||
|
||
expect(transactionSampleRateRt.decode(NaN).isRight()).toBe(false); | ||
}); | ||
|
||
it('checks whether the number of decimals is 3', () => { | ||
expect(transactionSampleRateRt.decode(1).isRight()).toBe(true); | ||
expect(transactionSampleRateRt.decode(0.99).isRight()).toBe(true); | ||
expect(transactionSampleRateRt.decode(0.999).isRight()).toBe(true); | ||
expect(transactionSampleRateRt.decode(0.998).isRight()).toBe(true); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
x-pack/legacy/plugins/apm/common/runtime_types/transaction_sample_rate_rt/index.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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import * as t from 'io-ts'; | ||
|
||
export const transactionSampleRateRt = new t.Type<number, number, unknown>( | ||
'TransactionSampleRate', | ||
t.number.is, | ||
(input, context) => { | ||
const value = Number(input); | ||
return value >= 0 && value <= 1 && Number(value.toFixed(3)) === value | ||
? t.success(value) | ||
: t.failure(input, context); | ||
}, | ||
t.identity | ||
); |
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
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.