UNPKG

4.58 kBJavaScriptView Raw
1const fetchWithNetworkError = require('@uppy/utils/lib/fetchWithNetworkError')
2
3/**
4 * A Barebones HTTP API client for Transloadit.
5 */
6module.exports = class Client {
7 constructor (opts = {}) {
8 this.opts = opts
9
10 this._reportError = this._reportError.bind(this)
11
12 this._headers = {
13 'Transloadit-Client': this.opts.client
14 }
15 }
16
17 /**
18 * Create a new assembly.
19 *
20 * @param {object} options
21 */
22 createAssembly ({
23 templateId,
24 params,
25 fields,
26 signature,
27 expectedFiles
28 }) {
29 const data = new FormData()
30 data.append('params', typeof params === 'string'
31 ? params
32 : JSON.stringify(params))
33 if (signature) {
34 data.append('signature', signature)
35 }
36
37 Object.keys(fields).forEach((key) => {
38 data.append(key, fields[key])
39 })
40 data.append('num_expected_upload_files', expectedFiles)
41
42 const url = `${this.opts.service}/assemblies`
43 return fetchWithNetworkError(url, {
44 method: 'post',
45 headers: this._headers,
46 body: data
47 })
48 .then((response) => response.json()).then((assembly) => {
49 if (assembly.error) {
50 const error = new Error(assembly.error)
51 error.details = assembly.message
52 error.assembly = assembly
53 if (assembly.assembly_id) {
54 error.details += ' ' + `Assembly ID: ${assembly.assembly_id}`
55 }
56 throw error
57 }
58
59 return assembly
60 })
61 .catch((err) => this._reportError(err, { url, type: 'API_ERROR' }))
62 }
63
64 /**
65 * Reserve resources for a file in an Assembly. Then addFile can be used later.
66 *
67 * @param {object} assembly
68 * @param {UppyFile} file
69 */
70 reserveFile (assembly, file) {
71 const size = encodeURIComponent(file.size)
72 const url = `${assembly.assembly_ssl_url}/reserve_file?size=${size}`
73 return fetchWithNetworkError(url, { method: 'post', headers: this._headers })
74 .then((response) => response.json())
75 .catch((err) => this._reportError(err, { assembly, file, url, type: 'API_ERROR' }))
76 }
77
78 /**
79 * Import a remote file to an Assembly.
80 *
81 * @param {object} assembly
82 * @param {UppyFile} file
83 */
84 addFile (assembly, file) {
85 if (!file.uploadURL) {
86 return Promise.reject(new Error('File does not have an `uploadURL`.'))
87 }
88 const size = encodeURIComponent(file.size)
89 const uploadUrl = encodeURIComponent(file.uploadURL)
90 const filename = encodeURIComponent(file.name)
91 const fieldname = 'file'
92
93 const qs = `size=${size}&filename=${filename}&fieldname=${fieldname}&s3Url=${uploadUrl}`
94 const url = `${assembly.assembly_ssl_url}/add_file?${qs}`
95 return fetchWithNetworkError(url, { method: 'post', headers: this._headers })
96 .then((response) => response.json())
97 .catch((err) => this._reportError(err, { assembly, file, url, type: 'API_ERROR' }))
98 }
99
100 /**
101 * Cancel a running Assembly.
102 *
103 * @param {object} assembly
104 */
105 cancelAssembly (assembly) {
106 const url = assembly.assembly_ssl_url
107 return fetchWithNetworkError(url, { method: 'delete', headers: this._headers })
108 .then((response) => response.json())
109 .catch((err) => this._reportError(err, { url, type: 'API_ERROR' }))
110 }
111
112 /**
113 * Get the current status for an assembly.
114 *
115 * @param {string} url The status endpoint of the assembly.
116 */
117 getAssemblyStatus (url) {
118 return fetchWithNetworkError(url, { headers: this._headers })
119 .then((response) => response.json())
120 .catch((err) => this._reportError(err, { url, type: 'STATUS_ERROR' }))
121 }
122
123 submitError (err, { endpoint, instance, assembly }) {
124 const message = err.details
125 ? `${err.message} (${err.details})`
126 : err.message
127
128 return fetchWithNetworkError('https://status.transloadit.com/client_error', {
129 method: 'post',
130 body: JSON.stringify({
131 endpoint,
132 instance,
133 assembly_id: assembly,
134 agent: typeof navigator !== 'undefined' ? navigator.userAgent : '',
135 client: this.opts.client,
136 error: message
137 })
138 })
139 .then((response) => response.json())
140 }
141
142 _reportError (err, params) {
143 if (this.opts.errorReporting === false) {
144 throw err
145 }
146
147 const opts = {
148 type: params.type
149 }
150 if (params.assembly) {
151 opts.assembly = params.assembly.assembly_id
152 opts.instance = params.assembly.instance
153 }
154 if (params.url) {
155 opts.endpoint = params.url
156 }
157
158 this.submitError(err, opts).catch((_) => {
159 // not much we can do then is there
160 })
161
162 throw err
163 }
164}