e98ce1a7e912053583181b1dded1403a6500f38a4c817d43173804c9c2394b4c22ea3c8bcdfe07c0dcd8d632fc2e351a358a21285bdc15a3d998ff5fef04ae 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import Dispatcher from './dispatcher'
  2. declare namespace MockCallHistoryLog {
  3. /** request's configuration properties */
  4. export type MockCallHistoryLogProperties = 'protocol' | 'host' | 'port' | 'origin' | 'path' | 'hash' | 'fullUrl' | 'method' | 'searchParams' | 'body' | 'headers'
  5. }
  6. /** a log reflecting request configuration */
  7. declare class MockCallHistoryLog {
  8. constructor (requestInit: Dispatcher.DispatchOptions)
  9. /** protocol used. ie. 'https:' or 'http:' etc... */
  10. protocol: string
  11. /** request's host. */
  12. host: string
  13. /** request's port. */
  14. port: string
  15. /** request's origin. ie. https://localhost:3000. */
  16. origin: string
  17. /** path. never contains searchParams. */
  18. path: string
  19. /** request's hash. */
  20. hash: string
  21. /** the full url requested. */
  22. fullUrl: string
  23. /** request's method. */
  24. method: string
  25. /** search params. */
  26. searchParams: Record<string, string>
  27. /** request's body */
  28. body: string | null | undefined
  29. /** request's headers */
  30. headers: Record<string, string | string[]> | null | undefined
  31. /** returns an Map of property / value pair */
  32. toMap (): Map<MockCallHistoryLog.MockCallHistoryLogProperties, string | Record<string, string | string[]> | null | undefined>
  33. /** returns a string computed with all key value pair */
  34. toString (): string
  35. }
  36. declare namespace MockCallHistory {
  37. export type FilterCallsOperator = 'AND' | 'OR'
  38. /** modify the filtering behavior */
  39. export interface FilterCallsOptions {
  40. /** the operator to apply when filtering. 'OR' will adds any MockCallHistoryLog matching any criteria given. 'AND' will adds only MockCallHistoryLog matching every criteria given. (default 'OR') */
  41. operator?: FilterCallsOperator | Lowercase<FilterCallsOperator>
  42. }
  43. /** a function to be executed for filtering MockCallHistoryLog */
  44. export type FilterCallsFunctionCriteria = (log: MockCallHistoryLog) => boolean
  45. /** parameter to filter MockCallHistoryLog */
  46. export type FilterCallsParameter = string | RegExp | undefined | null
  47. /** an object to execute multiple filtering at once */
  48. export interface FilterCallsObjectCriteria extends Record<string, FilterCallsParameter> {
  49. /** filter by request protocol. ie https: */
  50. protocol?: FilterCallsParameter;
  51. /** filter by request host. */
  52. host?: FilterCallsParameter;
  53. /** filter by request port. */
  54. port?: FilterCallsParameter;
  55. /** filter by request origin. */
  56. origin?: FilterCallsParameter;
  57. /** filter by request path. */
  58. path?: FilterCallsParameter;
  59. /** filter by request hash. */
  60. hash?: FilterCallsParameter;
  61. /** filter by request fullUrl. */
  62. fullUrl?: FilterCallsParameter;
  63. /** filter by request method. */
  64. method?: FilterCallsParameter;
  65. }
  66. }
  67. /** a call history to track requests configuration */
  68. declare class MockCallHistory {
  69. constructor (name: string)
  70. /** returns an array of MockCallHistoryLog. */
  71. calls (): Array<MockCallHistoryLog>
  72. /** returns the first MockCallHistoryLog */
  73. firstCall (): MockCallHistoryLog | undefined
  74. /** returns the last MockCallHistoryLog. */
  75. lastCall (): MockCallHistoryLog | undefined
  76. /** returns the nth MockCallHistoryLog. */
  77. nthCall (position: number): MockCallHistoryLog | undefined
  78. /** return all MockCallHistoryLog matching any of criteria given. if an object is used with multiple properties, you can change the operator to apply during filtering on options */
  79. filterCalls (criteria: MockCallHistory.FilterCallsFunctionCriteria | MockCallHistory.FilterCallsObjectCriteria | RegExp, options?: MockCallHistory.FilterCallsOptions): Array<MockCallHistoryLog>
  80. /** return all MockCallHistoryLog matching the given protocol. if a string is given, it is matched with includes */
  81. filterCallsByProtocol (protocol: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>
  82. /** return all MockCallHistoryLog matching the given host. if a string is given, it is matched with includes */
  83. filterCallsByHost (host: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>
  84. /** return all MockCallHistoryLog matching the given port. if a string is given, it is matched with includes */
  85. filterCallsByPort (port: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>
  86. /** return all MockCallHistoryLog matching the given origin. if a string is given, it is matched with includes */
  87. filterCallsByOrigin (origin: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>
  88. /** return all MockCallHistoryLog matching the given path. if a string is given, it is matched with includes */
  89. filterCallsByPath (path: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>
  90. /** return all MockCallHistoryLog matching the given hash. if a string is given, it is matched with includes */
  91. filterCallsByHash (hash: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>
  92. /** return all MockCallHistoryLog matching the given fullUrl. if a string is given, it is matched with includes */
  93. filterCallsByFullUrl (fullUrl: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>
  94. /** return all MockCallHistoryLog matching the given method. if a string is given, it is matched with includes */
  95. filterCallsByMethod (method: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>
  96. /** clear all MockCallHistoryLog on this MockCallHistory. */
  97. clear (): void
  98. /** use it with for..of loop or spread operator */
  99. [Symbol.iterator]: () => Generator<MockCallHistoryLog>
  100. }
  101. export { MockCallHistoryLog, MockCallHistory }