web-runtime.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getRuntime = void 0;
  4. /**
  5. * Disclaimer: modules in _shims aren't intended to be imported by SDK users.
  6. */
  7. const MultipartBody_1 = require("./MultipartBody.js");
  8. function getRuntime({ manuallyImported } = {}) {
  9. const recommendation = manuallyImported ?
  10. `You may need to use polyfills`
  11. : `Add one of these imports before your first \`import … from 'openai'\`:
  12. - \`import 'openai/shims/node'\` (if you're running on Node)
  13. - \`import 'openai/shims/web'\` (otherwise)
  14. `;
  15. let _fetch, _Request, _Response, _Headers;
  16. try {
  17. // @ts-ignore
  18. _fetch = fetch;
  19. // @ts-ignore
  20. _Request = Request;
  21. // @ts-ignore
  22. _Response = Response;
  23. // @ts-ignore
  24. _Headers = Headers;
  25. }
  26. catch (error) {
  27. throw new Error(`this environment is missing the following Web Fetch API type: ${error.message}. ${recommendation}`);
  28. }
  29. return {
  30. kind: 'web',
  31. fetch: _fetch,
  32. Request: _Request,
  33. Response: _Response,
  34. Headers: _Headers,
  35. FormData:
  36. // @ts-ignore
  37. typeof FormData !== 'undefined' ? FormData : (class FormData {
  38. // @ts-ignore
  39. constructor() {
  40. throw new Error(`file uploads aren't supported in this environment yet as 'FormData' is undefined. ${recommendation}`);
  41. }
  42. }),
  43. Blob: typeof Blob !== 'undefined' ? Blob : (class Blob {
  44. constructor() {
  45. throw new Error(`file uploads aren't supported in this environment yet as 'Blob' is undefined. ${recommendation}`);
  46. }
  47. }),
  48. File:
  49. // @ts-ignore
  50. typeof File !== 'undefined' ? File : (class File {
  51. // @ts-ignore
  52. constructor() {
  53. throw new Error(`file uploads aren't supported in this environment yet as 'File' is undefined. ${recommendation}`);
  54. }
  55. }),
  56. ReadableStream:
  57. // @ts-ignore
  58. typeof ReadableStream !== 'undefined' ? ReadableStream : (class ReadableStream {
  59. // @ts-ignore
  60. constructor() {
  61. throw new Error(`streaming isn't supported in this environment yet as 'ReadableStream' is undefined. ${recommendation}`);
  62. }
  63. }),
  64. getMultipartRequestOptions: async (
  65. // @ts-ignore
  66. form, opts) => ({
  67. ...opts,
  68. body: new MultipartBody_1.MultipartBody(form),
  69. }),
  70. getDefaultAgent: (url) => undefined,
  71. fileFromPath: () => {
  72. throw new Error('The `fileFromPath` function is only supported in Node. See the README for more details: https://www.github.com/openai/openai-node#file-uploads');
  73. },
  74. isFsReadStream: (value) => false,
  75. };
  76. }
  77. exports.getRuntime = getRuntime;
  78. //# sourceMappingURL=web-runtime.js.map