dataconnector.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { IDataConnector } from './interfaces';
  4. /**
  5. * An abstract class that adheres to the data connector interface.
  6. *
  7. * @typeparam T - The basic entity response type a service's connector.
  8. *
  9. * @typeparam U - The basic entity request type, which is conventionally the
  10. * same as the response type but may be different if a service's implementation
  11. * requires input data to be different from output responses. Defaults to `T`.
  12. *
  13. * @typeparam V - The basic token applied to a request, conventionally a string
  14. * ID or filter, but may be set to a different type when an implementation
  15. * requires it. Defaults to `string`.
  16. *
  17. * @typeparam W - The type of the optional `query` parameter of the `list`
  18. * method. Defaults to `string`.
  19. *
  20. * #### Notes
  21. * The only abstract method in this class is the `fetch` method, which must be
  22. * reimplemented by all subclasses. The `remove` and `save` methods have a
  23. * default implementation that returns a promise that will always reject. This
  24. * class is a convenience superclass for connectors that only need to `fetch`.
  25. */
  26. export abstract class DataConnector<T, U = T, V = string, W = string>
  27. implements IDataConnector<T, U, V, W> {
  28. /**
  29. * Retrieve an item from the data connector.
  30. *
  31. * @param id - The identifier used to retrieve an item.
  32. *
  33. * @returns A promise that resolves with a data payload if available.
  34. *
  35. * #### Notes
  36. * The promise returned by this method may be rejected if an error occurs in
  37. * retrieving the data. Nonexistence of an `id` will succeed with `undefined`.
  38. */
  39. abstract async fetch(id: V): Promise<T | undefined>;
  40. /**
  41. * Retrieve the list of items available from the data connector.
  42. *
  43. * @param query - The optional query filter to apply to the connector request.
  44. *
  45. * @returns A promise that always rejects with an error.
  46. *
  47. * #### Notes
  48. * Subclasses should reimplement if they support a back-end that can list.
  49. */
  50. async list(query?: W): Promise<{ ids: V[]; values: T[] }> {
  51. throw new Error('DataConnector#list method has not been implemented.');
  52. }
  53. /**
  54. * Remove a value using the data connector.
  55. *
  56. * @param id - The identifier for the data being removed.
  57. *
  58. * @returns A promise that always rejects with an error.
  59. *
  60. * #### Notes
  61. * Subclasses should reimplement if they support a back-end that can remove.
  62. */
  63. async remove(id: V): Promise<any> {
  64. throw new Error('DataConnector#remove method has not been implemented.');
  65. }
  66. /**
  67. * Save a value using the data connector.
  68. *
  69. * @param id - The identifier for the data being saved.
  70. *
  71. * @param value - The data being saved.
  72. *
  73. * @returns A promise that always rejects with an error.
  74. *
  75. * #### Notes
  76. * Subclasses should reimplement if they support a back-end that can save.
  77. */
  78. async save(id: V, value: U): Promise<any> {
  79. throw new Error('DataConnector#save method has not been implemented.');
  80. }
  81. }