Skip to content

Instantly share code, notes, and snippets.

@avtehnik
Created October 16, 2023 08:10
Show Gist options
  • Save avtehnik/b95e870e95d5e57abf8c692df616082b to your computer and use it in GitHub Desktop.
Save avtehnik/b95e870e95d5e57abf8c692df616082b to your computer and use it in GitHub Desktop.
node js ts vendure client example to createAssets
import crossFetch from 'cross-fetch';
import * as FormData from 'form-data';
import * as fs from "fs";
const formData = new FormData();
formData.append(
'operations',
JSON.stringify({
query: `
mutation createAssets($input: [CreateAssetInput!]!) {
createAssets(input: $input) {
... on Asset {
id
}
}
}
`,
variables: {
input: [
{
file: null,
},
],
},
}),
);
formData.append(
'map',
JSON.stringify({
'0': ['variables.input.0.file'],
}),
);
const filePath = '/Users/Downloads/1-200x300.jpg';
const stats = fs.statSync(filePath);
const fileStream = fs.createReadStream(filePath);
formData.append('0', fileStream, { knownLength: stats.size });
const response = await this.fetch(process.env.VENDURE_ADMIN_API_URL, {
method: 'POST',
body: formData,
credentials: 'include',
headers: {
...formData.getHeaders(),
},
});
const responseData = await response.json();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment