-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
64 lines (53 loc) · 1.45 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function createClients (buckets) {
const clients = buckets.map(({ name, client }) => `
const ${client} = {
bucketName: '${name},
async get (key) {
const response = await s3.getObject({ Bucket: this.bucketName, Key: key }).promise();
return {
contentType: response.ContentType,
eTag: response.ETag,
data: response.Body.toString(response.ContentType.includes('image') ? 'base64' : 'utf-8')
}
},
async put (key, blob) {
const ContentType = mime.lookup(key);
await s3.putObject({ Bucket: this.bucketName, Key: key, Body: blob, ContentType }).promise();
}
};
`);
const code = `
const S3 = require('aws-sdk/clients/s3');
const mime = require('mime-types');
const runningLocally = process.env.NODE_ENV === 'testing';
const awsConfig = runningLocally ? {
accessKeyId: 'S3RVER',
secretAccessKey: 'S3RVER',
endpoint: 'http://127.0.0.1:4569',
s3ForcePathStyle: true
} : {};
const s3 = new S3(awsConfig);
${clients.join('\n')}
const buckets = {
${buckets.map(({ client }) => client).join(', ')}
};
module.exports = buckets;
`;
const types = `
declare type client = {
bucketName: string;
get(key: any): Promise<{
contentType: string;
eTag: string;
data: string;
}>;
put(key: any, blob: any): Promise<void>;
};
declare const buckets: {
${buckets.map(({ client }) => `${client}: client`).join(', ')}, awsConfig
};
export = buckets
`;
return { code, types };
}
module.exports = createClients;