Tools for learning Japanese.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.8 KiB

  1. import * as SupportedSources from './sources';
  2. import * as SupportedAdapters from './adapters';
  3. export * as Sources from './sources';
  4. export * as Adapters from './adapters';
  5. export * from './streams';
  6. export type CreateDownloaderParams = (
  7. SupportedSources.Kanjidic.CreateDownloaderParams
  8. | SupportedSources.JMdict.CreateDownloaderParams
  9. | SupportedSources.JMnedict.CreateDownloaderParams
  10. | SupportedSources.RadKFile.CreateDownloaderParams
  11. | SupportedSources.KRadFile.CreateDownloaderParams
  12. );
  13. export const createDownloader = (params: CreateDownloaderParams) => {
  14. const { type: sourceType, ...etcParams } = params;
  15. const theSupportedSources = Object.values(SupportedSources);
  16. const theSourceModule = theSupportedSources
  17. .find((videoTypeModule) => videoTypeModule.SOURCE_ID === sourceType);
  18. if (!theSourceModule) {
  19. const validSourceTypes = theSupportedSources.map((videoTypeModule) => videoTypeModule.SOURCE_ID).join(', ');
  20. throw new TypeError(`Invalid source type: "${sourceType}". Valid values are: ${validSourceTypes}`);
  21. }
  22. return theSourceModule.createDownloader(etcParams);
  23. };
  24. export type CreateAdapterParams = (
  25. SupportedAdapters.FileJsonl.CreateAdapterParams
  26. );
  27. export const createAdapter = (params: CreateAdapterParams) => {
  28. const { type: adapterType, ...etcParams } = params;
  29. const theSupportedAdapters = Object.values(SupportedAdapters);
  30. const theAdapterModule = theSupportedAdapters
  31. .find((adapterTypeModule) => adapterTypeModule.ADAPTER_ID === adapterType);
  32. if (!theAdapterModule) {
  33. const validAdapterTypes = theSupportedAdapters.map((adapterTypeModule) => adapterTypeModule.ADAPTER_ID).join(', ');
  34. throw new TypeError(`Invalid adapter type: "${adapterType}". Valid values are: ${validAdapterTypes}`);
  35. }
  36. return theAdapterModule.createAdapter(etcParams);
  37. };