Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Mar 11, 2021
1 parent 094439c commit 03fda2f
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 137 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ wrangler secret put AUTH0_DOMAIN # Get from auth0 account
wrangler secret put AUTH0_CLIENT_ID # Get from auth0 account
wrangler secret put AUTH0_CLIENT_SECRET # Get from auth0 account
wrangler secret put SALT # open `https://csprng.xyz/v1/api` in the browser and use the value of `Data`
wrangler secret put SALT # Get from Pinata
wrangler secret put PINATA_JWT # Get from Pinata
```
Go to `/site/src/constants.js` *comment* the first line and run `wrangler publish`.

Expand All @@ -75,7 +75,7 @@ wrangler secret put AUTH0_DOMAIN --env production # Get from auth0 account
wrangler secret put AUTH0_CLIENT_ID --env production # Get from auth0 account
wrangler secret put AUTH0_CLIENT_SECRET --env production # Get from auth0 account
wrangler secret put SALT --env production # open `https://csprng.xyz/v1/api` in the browser and use the value of `Data`
wrangler secret put SALT --env production # Get from Pinata
wrangler secret put PINATA_JWT --env production # Get from Pinata
wrangler publish --env production
```

Expand Down
3 changes: 2 additions & 1 deletion site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"devDependencies": {
"@cloudflare/workers-types": "^2.1.0",
"@types/auth0-js": "^9.14.2",
"@types/cookie": "^0.4.0"
"@types/cookie": "^0.4.0",
"prettier": "^2.2.1"
},
"prettier": {
"trailingComma": "es5",
Expand Down
22 changes: 11 additions & 11 deletions site/src/constants.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// let AUTH0_DOMAIN, AUTH0_CLIENT_ID, AUTH0_CLIENT_SECRET, SALT
export const stores = {
auth: SESSION,
csrf: CSRF,
users: USERS,
nfts: NFTS
auth: SESSION,
csrf: CSRF,
users: USERS,
nfts: NFTS,
}

export const auth0 = {
domain: AUTH0_DOMAIN,
clientId: AUTH0_CLIENT_ID,
clientSecret: AUTH0_CLIENT_SECRET,
callbackUrl: AUTH0_CALLBACK_URL,
salt: SALT
};
domain: AUTH0_DOMAIN,
clientId: AUTH0_CLIENT_ID,
clientSecret: AUTH0_CLIENT_SECRET,
callbackUrl: AUTH0_CALLBACK_URL,
salt: SALT,
}

export const pinata = {
jwt: PINATA_JWT
jwt: PINATA_JWT,
}

export const cookieKey = 'AUTH0-AUTH'
Expand Down
26 changes: 13 additions & 13 deletions site/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ class HTTPError extends Error {
* @param {Error & { status?: number }} error
*/
static respond(error) {
return new Response(JSON.stringify({
ok: false,
error: {
name: error.name,
message: error.message
return new Response(
JSON.stringify({
ok: false,
error: {
name: error.name,
message: error.message,
},
}),
{
statusText: error.message,
status: error.status || 500,
}
}), {
statusText: error.message,
status: error.status || 500,
})
)
}
}


export {
HTTPError
}
export { HTTPError }
12 changes: 5 additions & 7 deletions site/src/models/nfts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import { stores } from '../constants.js'
/**
* @param {Key} key
*/
const encodeKey = ({user, cid}) => `${user.sub}:${cid}`

const encodeKey = ({ user, cid }) => `${user.sub}:${cid}`

/**
* @param {Key} key
* @param {Key} key
* @returns {Promise<boolean>}
*/
export const has = async (key) => {
return null == await stores.nfts.get(encodeKey(key))
return null == (await stores.nfts.get(encodeKey(key)))
}

/**
Expand All @@ -29,7 +28,7 @@ export const set = async (key, value) => {
}

/**
* @param {Key} key
* @param {Key} key
* @returns {Promise<NFT|null>}
*/
export const get = async (key) => {
Expand All @@ -42,7 +41,6 @@ export const get = async (key) => {
}

/**
* @param {Key} key
* @param {Key} key
*/
export const remove = async (key) => stores.nfts.delete(encodeKey(key))

2 changes: 1 addition & 1 deletion site/src/models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const users = USERS
* email?: string
* picture?: string
* }} User
*
*
* @expample
* ```json
* {
Expand Down
19 changes: 11 additions & 8 deletions site/src/pinata.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pinata } from "./constants.js"
import { pinata } from './constants.js'

const endpoint = new URL('https://api.pinata.cloud')

Expand All @@ -10,19 +10,22 @@ const endpoint = new URL('https://api.pinata.cloud')
export const pinFile = async (blob) => {
const body = new FormData()
body.append('file', blob)
body.append('pinataMetadata', JSON.stringify({
keyvalues: {
origin: 'https://nft.storage/'
}
}))
body.append(
'pinataMetadata',
JSON.stringify({
keyvalues: {
origin: 'https://nft.storage/',
},
})
)
const url = new URL('/pinning/pinFileToIPFS', endpoint)

const response = await fetch(url.toString(), {
body,
method: 'POST',
headers: {
'authorization': `Bearer ${pinata.jwt}`
}
authorization: `Bearer ${pinata.jwt}`,
},
})

if (response.ok) {
Expand Down
20 changes: 11 additions & 9 deletions site/src/routes/delete.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { HTTPError } from '../errors.js'
import { verifyToken, parseRequestCID } from '../utils/utils.js'
import * as nfts from "../models/nfts.js"
import * as nfts from '../models/nfts.js'

/**
* @param {FetchEvent} event
* @returns {Promise<Response>}
*/
export const remove = async event => {
export const remove = async (event) => {
const authResult = await verifyToken(event)
if (!authResult.ok) {
return HTTPError.respond(authResult.error)
Expand All @@ -18,17 +18,19 @@ export const remove = async event => {
}
const cid = parseResult.value


await nfts.remove({ user, cid })
// TODO: We need to unpin from pinata as well, however we need to make
// no other user has pinned same CID, which makes me wonder if KV store
// has eventual or strong consistency. If former we might have problems.

return new Response(JSON.stringify({
ok: true
}), {
headers: {
'content-type': 'application/json;charset=UTF-8'
return new Response(
JSON.stringify({
ok: true,
}),
{
headers: {
'content-type': 'application/json;charset=UTF-8',
},
}
})
)
}
4 changes: 2 additions & 2 deletions site/src/routes/logout.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { logoutHeaders, getAsset } from "../utils/utils.js"
import { logoutHeaders, getAsset } from '../utils/utils.js'

/**
* Logout route
*
*
* @param {FetchEvent} event
*/
export async function logout(event) {
Expand Down
41 changes: 23 additions & 18 deletions site/src/routes/status.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { HTTPError } from '../errors.js'
import { verifyToken, parseRequestCID } from '../utils/utils.js'
import * as nfts from "../models/nfts.js"
import * as nfts from '../models/nfts.js'

/**
* @param {FetchEvent} event
* @returns {Promise<Response>}
*/
export const status = async event => {
export const status = async (event) => {
const authResult = await verifyToken(event)
if (!authResult.ok) {
return HTTPError.respond(authResult.error)
Expand All @@ -18,26 +18,31 @@ export const status = async event => {
}
const cid = parseResult.value


const status = await nfts.get({ user, cid })
if (status) {
return new Response(JSON.stringify({
ok: true,
value: status
}), {
headers: {
'content-type': 'application/json;charset=UTF-8'
return new Response(
JSON.stringify({
ok: true,
value: status,
}),
{
headers: {
'content-type': 'application/json;charset=UTF-8',
},
}
})
)
} else {
return new Response(JSON.stringify({
ok: false,
error: { message: `NFT with a CID ${cid} not found` }
}), {
status: 404,
headers: {
'content-type': 'application/json;charset=UTF-8'
return new Response(
JSON.stringify({
ok: false,
error: { message: `NFT with a CID ${cid} not found` },
}),
{
status: 404,
headers: {
'content-type': 'application/json;charset=UTF-8',
},
}
})
)
}
}
37 changes: 19 additions & 18 deletions site/src/routes/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HTTPError } from '../errors.js'
import { verifyToken, hash } from '../utils/utils.js'
import { parseMultipart } from '../utils/multipart/index.js'
import * as pinata from '../pinata.js'
import * as nfts from "../models/nfts.js"
import * as nfts from '../models/nfts.js'

/**
* @param {FetchEvent} event
Expand Down Expand Up @@ -39,29 +39,30 @@ export async function upload(event) {
const pin = await pinata.pinFile(blob)
if (pin.ok) {
const { IpfsHash: cid, Timestamp: created } = pin.value
//
//
if (await nfts.has({ user, cid })) {
await nfts.set({ user, cid }, {
cid,
deals: { status: 'ongoing', deals: [] },
pin: {
await nfts.set(
{ user, cid },
{
cid,
status: "pinned",
deals: { status: 'ongoing', deals: [] },
pin: {
cid,
status: 'pinned',
// @ts-expect-error - TODO: Define encoded types.
created,
},
// @ts-expect-error - TODO: Define encoded types.
created
},
// @ts-expect-error - TODO: Define encoded types.
created
})
created,
}
)
}


return new Response(JSON.stringify({ ok: true, value: { cid } }), {
headers: {
'content-type': 'application/json;charset=UTF-8',
},
}
)
headers: {
'content-type': 'application/json;charset=UTF-8',
},
})
} else {
return HTTPError.respond(new HTTPError(pin.error.statusText))
}
Expand Down
2 changes: 1 addition & 1 deletion site/src/utils/auth0.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const authorize = async (event) => {
* @property {string} accessToken
* @property {string} idToken
* @property {import('./jwt').JWT} userInfo
*
*
* Verify our application’s users based on the Cookie field and make any authorization information * * available as part of the authorization object
*
* @param {FetchEvent} event
Expand Down
Loading

0 comments on commit 03fda2f

Please sign in to comment.