Skip to content

Commit bbcda07

Browse files
committed
feat: done
1 parent 7f67553 commit bbcda07

File tree

7 files changed

+4181
-28
lines changed

7 files changed

+4181
-28
lines changed

package.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,24 @@
2424
},
2525
"homepage": "https://github.com/kobra-dev/dataset-api#readme",
2626
"dependencies": {
27+
"@types/validator": "^13.1.3",
28+
"aws-sdk": "^2.903.0",
2729
"fastify": "^3.0.0",
2830
"fastify-autoload": "^3.3.1",
2931
"fastify-cli": "^2.11.0",
3032
"fastify-env": "^2.1.1",
33+
"fastify-file-upload": "^3.0.0",
34+
"fastify-multipart": "^4.0.5",
3135
"fastify-plugin": "^3.0.0",
32-
"fastify-sensible": "^3.1.0"
36+
"fastify-sensible": "^3.1.0",
37+
"uuid": "^8.3.2",
38+
"validator": "^13.6.0"
3339
},
3440
"devDependencies": {
41+
"@types/busboy": "^0.2.3",
3542
"@types/node": "15.0.2",
3643
"@types/tap": "15.0.1",
44+
"@types/uuid": "^8.3.0",
3745
"@typescript-eslint/eslint-plugin": "4.22.1",
3846
"@typescript-eslint/parser": "4.22.1",
3947
"concurrently": "6.1.0",

src/app.ts

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
11
import { FastifyPluginAsync } from 'fastify'
22
import AutoLoad, { AutoloadPluginOptions } from 'fastify-autoload'
3-
// import fastifyEnv from 'fastify-env'
3+
import fastifyMultipart from 'fastify-multipart'
44
import { join } from 'path'
55

66
export type AppOptions = {} & Partial<AutoloadPluginOptions>
77

8-
// const schema = {
9-
// type: 'object',
10-
// require: ['PORT'],
11-
// properties: {
12-
// PPORT: {
13-
// type: 'string',
14-
// default: '3000',
15-
// },
16-
// },
17-
// }
18-
198
const app: FastifyPluginAsync<AppOptions> = async (
209
fastify,
2110
opts,
2211
): Promise<void> => {
23-
// void fastify.register(fastifyEnv, {
24-
// schema,
25-
// })
12+
void fastify.register(fastifyMultipart, { attachFieldsToBody: true })
2613

2714
void fastify.register(AutoLoad, {
2815
dir: join(__dirname, 'plugins'),

src/routes/dataset.ts

+66-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,76 @@
1+
import S3 from 'aws-sdk/clients/s3'
12
import { FastifyPluginAsync } from 'fastify'
3+
import http from 'http'
4+
import { v4 as uuid } from 'uuid'
5+
import isURL from 'validator/lib/isURL'
26

3-
const example: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
4-
fastify.post('/', async function (request, reply) {
5-
return 'this is an example'
7+
const datasets: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
8+
const s3 = new S3({
9+
endpoint: process.env.API_END_POINT,
10+
accessKeyId: process.env.ACCESS_KEY,
11+
secretAccessKey: process.env.SECRET_KEY,
612
})
713

8-
fastify.post('/:url', async function (request, reply) {
9-
return 'read from the url'
14+
fastify.post('/dataset', async function (request: any, reply) {
15+
const encodedDataset = await request.body.upload
16+
.toBuffer()
17+
.toString('base64')
18+
19+
const params = {
20+
Body: encodedDataset,
21+
Bucket: 'kobra',
22+
Key: uuid(),
23+
}
24+
25+
s3.putObject(params, function (err: any, data: any) {
26+
if (err) throw new Error(err)
27+
})
28+
29+
reply.status(201).send({
30+
message: 'file uploaded successfully',
31+
key: params.Key,
32+
})
1033
})
1134

12-
fastify.get('/:id', async function (request, reply) {
13-
console.log(request.params)
35+
fastify.get('/dataset/:key', async function (request: any, reply) {
36+
const params = {
37+
Bucket: 'kobra',
38+
Key: request.params.key,
39+
}
40+
41+
//TODO: fix loading the buffer
42+
s3.getObject(params, async function (err, data) {
43+
if (err) reply.badRequest(err.message)
44+
45+
let dataset: any = data.Body
46+
47+
try {
48+
dataset = await dataset.toString('utf-8')
49+
reply.send({ dataset })
50+
} catch (error) {}
51+
})
52+
53+
reply.status(200).send({ success: true, params })
54+
})
55+
56+
fastify.post('/dataset/url', async function (request: any, reply) {
57+
const { url } = request.body
58+
59+
if (!isURL(url))
60+
reply
61+
.status(404)
62+
.send({ sucess: true, message: 'Invalid url passed' })
63+
64+
//TODO: fix file downloading
65+
const file = http.get(url, function (resp) {
66+
console.log(resp)
67+
})
68+
console.log({ file })
1469

15-
return 'Hello world'
70+
reply.status(200).send({
71+
success: true,
72+
})
1673
})
1774
}
1875

19-
export default example
76+
export default datasets

src/routes/root.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { FastifyPluginAsync } from 'fastify'
22

33
const root: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
44
fastify.get('/', async function (request, reply) {
5-
return { root: true }
5+
return { message: 'Hell from the APi' }
66
})
77
}
88

test/routes/root.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ test('default root route', async (t) => {
77
const res = await app.inject({
88
url: '/',
99
})
10-
t.same(JSON.parse(res.payload), { root: true })
10+
t.same(JSON.parse(res.payload), { message: 'Hell from the APi' })
1111
})

tsconfig.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"extends": "fastify-tsconfig",
3+
34
"compilerOptions": {
4-
"outDir": "dist"
5+
"outDir": "dist",
6+
"esModuleInterop": true
57
},
68
"include": ["src/**/*.ts"]
79
}

0 commit comments

Comments
 (0)