-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
[APM] migrate to io-ts #42961
Merged
Merged
[APM] migrate to io-ts #42961
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f19d81e
[APM] migrate to io-ts
dgieselaar 87ea35b
Migrate remaining routes to io-ts
dgieselaar 32c5beb
Infer response type for useFetcher()
dgieselaar 3ff0922
Review feedback
dgieselaar 3dee615
Use createRangeType util
dgieselaar f8682fa
Extract & test runtime types
dgieselaar 056490b
Simplify runtime types
dgieselaar f4c6d02
Tests for createApi and callApmApi
dgieselaar 9445ffe
Use more readable variable names in runtime types
dgieselaar 0315862
Remove UIFilters query param for API endpoints where it is not supported
dgieselaar 3184812
Fix issues w/ default parameters in create_api
dgieselaar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Number(value.toFixed(3)) === value
looks so much nicer than the one I suggested 👍