files.mjs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. import { APIResource } from "../../core/resource.mjs";
  3. import { Page } from "../../core/pagination.mjs";
  4. import { buildHeaders } from "../../internal/headers.mjs";
  5. import { stainlessHelperHeaderFromFile } from "../../lib/stainless-helper-header.mjs";
  6. import { multipartFormRequestOptions } from "../../internal/uploads.mjs";
  7. import { path } from "../../internal/utils/path.mjs";
  8. export class Files extends APIResource {
  9. /**
  10. * List Files
  11. *
  12. * @example
  13. * ```ts
  14. * // Automatically fetches more pages as needed.
  15. * for await (const fileMetadata of client.beta.files.list()) {
  16. * // ...
  17. * }
  18. * ```
  19. */
  20. list(params = {}, options) {
  21. const { betas, ...query } = params ?? {};
  22. return this._client.getAPIList('/v1/files', (Page), {
  23. query,
  24. ...options,
  25. headers: buildHeaders([
  26. { 'anthropic-beta': [...(betas ?? []), 'files-api-2025-04-14'].toString() },
  27. options?.headers,
  28. ]),
  29. });
  30. }
  31. /**
  32. * Delete File
  33. *
  34. * @example
  35. * ```ts
  36. * const deletedFile = await client.beta.files.delete(
  37. * 'file_id',
  38. * );
  39. * ```
  40. */
  41. delete(fileID, params = {}, options) {
  42. const { betas } = params ?? {};
  43. return this._client.delete(path `/v1/files/${fileID}`, {
  44. ...options,
  45. headers: buildHeaders([
  46. { 'anthropic-beta': [...(betas ?? []), 'files-api-2025-04-14'].toString() },
  47. options?.headers,
  48. ]),
  49. });
  50. }
  51. /**
  52. * Download File
  53. *
  54. * @example
  55. * ```ts
  56. * const response = await client.beta.files.download(
  57. * 'file_id',
  58. * );
  59. *
  60. * const content = await response.blob();
  61. * console.log(content);
  62. * ```
  63. */
  64. download(fileID, params = {}, options) {
  65. const { betas } = params ?? {};
  66. return this._client.get(path `/v1/files/${fileID}/content`, {
  67. ...options,
  68. headers: buildHeaders([
  69. {
  70. 'anthropic-beta': [...(betas ?? []), 'files-api-2025-04-14'].toString(),
  71. Accept: 'application/binary',
  72. },
  73. options?.headers,
  74. ]),
  75. __binaryResponse: true,
  76. });
  77. }
  78. /**
  79. * Get File Metadata
  80. *
  81. * @example
  82. * ```ts
  83. * const fileMetadata =
  84. * await client.beta.files.retrieveMetadata('file_id');
  85. * ```
  86. */
  87. retrieveMetadata(fileID, params = {}, options) {
  88. const { betas } = params ?? {};
  89. return this._client.get(path `/v1/files/${fileID}`, {
  90. ...options,
  91. headers: buildHeaders([
  92. { 'anthropic-beta': [...(betas ?? []), 'files-api-2025-04-14'].toString() },
  93. options?.headers,
  94. ]),
  95. });
  96. }
  97. /**
  98. * Upload File
  99. *
  100. * @example
  101. * ```ts
  102. * const fileMetadata = await client.beta.files.upload({
  103. * file: fs.createReadStream('path/to/file'),
  104. * });
  105. * ```
  106. */
  107. upload(params, options) {
  108. const { betas, ...body } = params;
  109. return this._client.post('/v1/files', multipartFormRequestOptions({
  110. body,
  111. ...options,
  112. headers: buildHeaders([
  113. { 'anthropic-beta': [...(betas ?? []), 'files-api-2025-04-14'].toString() },
  114. stainlessHelperHeaderFromFile(body.file),
  115. options?.headers,
  116. ]),
  117. }, this._client));
  118. }
  119. }
  120. //# sourceMappingURL=files.mjs.map