Skip to content

Commit 2e25bbc

Browse files
committed
version 0.0.1
1 parent f156946 commit 2e25bbc

File tree

8 files changed

+299
-1
lines changed

8 files changed

+299
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
lib
3+
*.tgz

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
src/
2+
*.tgz
3+
tsconfig.json

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,39 @@
1-
# relay-middleware-upload-s3
1+
# [React Relay Upload S3](https://github.com/morrys/react-relay-upload-s3)
22
Middleware for Relay Modern Network Layer for upload file to AWS S3
3+
4+
## Installation
5+
6+
Install react-relay-upload-s3 using yarn or npm:
7+
8+
```
9+
yarn add react-relay-upload-s3
10+
```
11+
12+
## Usage
13+
14+
```typescript
15+
import {uploadS3Middleware} from 'react-relay-upload-s3';
16+
```
17+
18+
```typescript
19+
const network = new RelayNetworkLayer(
20+
[
21+
urlMiddleware(...),
22+
uploadS3Middleware({credentials: complexObjectsCredentials}),
23+
authMiddleware(...),
24+
],
25+
{}
26+
);
27+
```
28+
29+
## TODO
30+
31+
Document how to use urlMiddleware and authMiddleware in the context of AWS
32+
33+
Implementation of Middleware for IAM Authentication
34+
35+
36+
## License
37+
38+
React Relay Upload S3 is [MIT licensed](./LICENSE).
39+

package-lock.json

Lines changed: 128 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "react-relay-upload-s3",
3+
"version": "0.0.1",
4+
"keywords": [
5+
"graphql",
6+
"relay",
7+
"upload",
8+
"react",
9+
"aws",
10+
"appsync",
11+
"s3"
12+
],
13+
"main": "lib/index.js",
14+
"license": "SEE LICENSE IN LICENSE",
15+
"description": "React Relay Middleware Upload S3",
16+
"author": {
17+
"name": "morrys"
18+
},
19+
"homepage": "https://github.com/morrys/react-relay-upload-s3",
20+
"repository": {
21+
"type": "git",
22+
"url": "https://github.com/morrys/react-relay-upload-s3"
23+
},
24+
"scripts": {
25+
"prepare": "tsc"
26+
},
27+
"dependencies": {},
28+
"peerDependencies": {
29+
"react-relay": ">=1.4.0"
30+
},
31+
"devDependencies": {
32+
"typescript": "3.1.1",
33+
"aws-sdk": "^2.449.0",
34+
"react-relay-network-modern": "^2.5.1"
35+
}
36+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default as uploadS3Middleware} from "./middleware-upload-s3";

src/middleware-upload-s3.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { Middleware, RelayNetworkLayerRequest } from 'react-relay-network-modern/lib/definition';
2+
import { Credentials, CredentialsOptions } from 'aws-sdk/lib/credentials';
3+
import * as S3 from 'aws-sdk/clients/s3';
4+
5+
type CredentialsGetter = () => (Credentials | CredentialsOptions | Promise<Credentials> | Promise<CredentialsOptions> | null) | Credentials | CredentialsOptions | Promise<Credentials> | Promise<CredentialsOptions> | null;
6+
7+
export type UploadS3MiddlewareOpts = {
8+
credentials: CredentialsGetter
9+
};
10+
11+
export interface FileS3Type {
12+
bucket: string
13+
key: string
14+
region: string
15+
file: File | Blob
16+
}
17+
18+
export interface UploadableS3 {
19+
[key: string]: FileS3Type;
20+
}
21+
22+
function upload(fileField: FileS3Type, { credentials }) {
23+
const Body = fileField.file;
24+
const { type: ContentType } = Body;
25+
26+
const {
27+
bucket: Bucket,
28+
key: Key,
29+
region,
30+
} = fileField;
31+
const s3 = new S3({
32+
credentials,
33+
region,
34+
});
35+
36+
return s3.upload({
37+
Bucket,
38+
Key,
39+
Body,
40+
ContentType,
41+
}).promise();
42+
};
43+
44+
export default function uploadS3Middleware(opts: UploadS3MiddlewareOpts): Middleware {
45+
const {
46+
credentials,
47+
} = opts;
48+
return (next) => async (req: RelayNetworkLayerRequest) => {
49+
const uploadables: UploadableS3 = req.uploadables;
50+
if (uploadables) {
51+
const uploadCredentials = typeof credentials === 'function' ? (credentials as any).call() : credentials;
52+
const uploadPromise = Promise.resolve(uploadCredentials)
53+
.then(credentials => {
54+
const uploadPromises = Object.entries(uploadables).map(([_, fileUpload]) => upload(fileUpload, { credentials }));
55+
return Promise.all([...uploadPromises] as Promise<any>[]);
56+
}).then(() => {
57+
delete req.uploadables;
58+
req.fetchOpts.body = req.prepareBody();
59+
return next(req)
60+
})
61+
return uploadPromise;
62+
} else {
63+
return next(req)
64+
}
65+
};
66+
}

tsconfig.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "lib",
4+
"rootDir": "src",
5+
"module": "commonjs",
6+
"target": "es5",
7+
"moduleResolution": "node",
8+
"noEmitOnError": true,
9+
"declaration": true,
10+
"lib": [
11+
"dom",
12+
"es6",
13+
"esnext.asynciterable",
14+
"es2017.object"
15+
],
16+
"jsx": "react",
17+
"skipLibCheck": true
18+
},
19+
"exclude": [
20+
"lib",
21+
"__tests__"
22+
],
23+
"compileOnSave": true
24+
}

0 commit comments

Comments
 (0)