-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/doctypes #6
Changes from 4 commits
4e5b8c5
158870b
8351fea
e4b4863
2aea48e
04e4267
bf9da95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
declare module 'identity-wallet' { | ||
export default class IdentityWallet { | ||
constructor (getConsent: () => Promise<boolean>, opts: any) | ||
|
||
get3idProvider (): any; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import ipfsClient from 'ipfs-http-client' | ||
import express, { Request, Response, NextFunction } from 'express' | ||
import IdentityWallet from 'identity-wallet' | ||
import Ceramic from '@ceramicnetwork/ceramic-core' | ||
|
||
const IPFS_HOST = 'http://localhost:5001' | ||
const toApiPath = (ending: string):string => '/api/v0' + ending | ||
// TODO - don't hardcode seed lol | ||
const seed = '0x5872d6e0ae7347b72c9216db218ebbb9d9d0ae7ab818ead3557e8e78bf944184' | ||
|
||
class CeramicDaemon { | ||
private app: any | ||
|
@@ -16,53 +19,73 @@ class CeramicDaemon { | |
next() | ||
}) | ||
this.app.post(toApiPath('/create'), this.createDoc.bind(this)) | ||
this.app.get(toApiPath('/show/ceramic/:doctype/:cid'), this.show.bind(this)) | ||
this.app.get(toApiPath('/state/ceramic/:doctype/:cid'), this.state.bind(this)) | ||
this.app.post(toApiPath('/change/ceramic/:doctype/:cid'), this.change.bind(this)) | ||
this.app.get(toApiPath('/show/ceramic/:cid'), this.show.bind(this)) | ||
this.app.get(toApiPath('/state/ceramic/:cid'), this.state.bind(this)) | ||
this.app.post(toApiPath('/change/ceramic/:cid'), this.change.bind(this)) | ||
const server = this.app.listen(7007, () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can put a daemon There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, that should be configurable once we have a config file! |
||
console.log('Ceramic API running on port 7007') | ||
console.log('User DID:', ceramic.user.DID) | ||
}) | ||
server.keepAliveTimeout = 60 * 1000 | ||
} | ||
|
||
static async create (ipfsHost: string = IPFS_HOST) { | ||
const ipfs = ipfsClient(ipfsHost) | ||
const ceramic = await Ceramic.create(ipfs) | ||
const idWallet = new IdentityWallet(async () => true, { seed }) | ||
await ceramic.setDIDProvider(idWallet.get3idProvider()) | ||
return new CeramicDaemon(ceramic) | ||
} | ||
|
||
async createDoc (req: Request, res: Response, next: NextFunction) { | ||
const { doctype, genesis, onlyGenesis } = req.body | ||
if (!doctype || !genesis) {} // TODO - reject somehow | ||
const doc = await this.ceramic.createDocument(genesis, doctype, { onlyGenesis }) | ||
res.json({ docId: doc.id, state: doc.state }) | ||
try { | ||
const doc = await this.ceramic.createDocument(genesis, doctype, { onlyGenesis }) | ||
res.json({ docId: doc.id, state: doc.state }) | ||
} catch (e) { | ||
res.json({ error: e.message }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to handle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, right now this isn't really using best practices for express or rest apis in general. We should make sure to do this properly! |
||
} | ||
next() | ||
} | ||
|
||
async show (req: Request, res: Response, next: NextFunction) { | ||
const { doctype, cid } = req.params | ||
const docId = ['/ceramic', doctype, cid].join('/') | ||
const doc = await this.ceramic.loadDocument(docId) | ||
const docId = ['/ceramic', req.params.cid].join('/') | ||
try { | ||
const doc = await this.ceramic.loadDocument(docId) | ||
res.json({ docId: doc.id, content: doc.content }) | ||
} catch (e) { | ||
res.json({ error: e.message }) | ||
} | ||
next() | ||
} | ||
|
||
async state (req: Request, res: Response, next: NextFunction) { | ||
const { doctype, cid } = req.params | ||
const docId = ['/ceramic', doctype, cid].join('/') | ||
const doc = await this.ceramic.loadDocument(docId) | ||
res.json({ docId: doc.id, state: doc.state }) | ||
const docId = ['/ceramic', req.params.cid].join('/') | ||
try { | ||
const doc = await this.ceramic.loadDocument(docId) | ||
res.json({ docId: doc.id, state: doc.state }) | ||
} catch (e) { | ||
res.json({ error: e.message }) | ||
} | ||
next() | ||
} | ||
|
||
async change (req: Request, res: Response, next: NextFunction) { | ||
const { content } = req.body | ||
if (!content) {} // TODO - reject somehow | ||
const { doctype, cid } = req.params | ||
const docId = ['/ceramic', doctype, cid].join('/') | ||
const doc = await this.ceramic.loadDocument(docId) | ||
await doc.change(content) | ||
res.json({ docId: doc.id, state: doc.state }) | ||
if (!content) { | ||
res.json({ error: 'content required to change document' }) | ||
next() | ||
return | ||
} | ||
const docId = ['/ceramic', req.params.cid].join('/') | ||
try { | ||
const doc = await this.ceramic.loadDocument(docId) | ||
await doc.change(content) | ||
res.json({ docId: doc.id, state: doc.state }) | ||
} catch (e) { | ||
res.json({ error: e.message }) | ||
} | ||
next() | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import CeramicDaemon from './ceramic-daemon' | |
import CeramicClient from './ceramic-client' | ||
import program from 'commander' | ||
|
||
|
||
program | ||
.command('daemon') | ||
.option('--ipfs-api <url>', 'The ipfs http api to use') | ||
|
@@ -18,9 +19,13 @@ program | |
.action(async (doctype, content, { onlyGenesis }) => { | ||
content = JSON.parse(content) | ||
const ceramic = new CeramicClient() | ||
const doc = await ceramic.createDocument(content, doctype, { onlyGenesis }) | ||
console.log(doc.id) | ||
console.log(JSON.stringify(doc.content, null, 2)) | ||
try { | ||
const doc = await ceramic.createDocument(content, doctype, { onlyGenesis }) | ||
console.log(doc.id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can introduce a specific logger (e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely, created an issue here: #8 |
||
console.log(JSON.stringify(doc.content, null, 2)) | ||
} catch (e) { | ||
console.error(e) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that makes sense! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
ceramic.close() | ||
}) | ||
|
||
|
@@ -30,8 +35,12 @@ program | |
.description('Show the content of a document') | ||
.action(async (docId) => { | ||
const ceramic = new CeramicClient() | ||
const doc = await ceramic.loadDocument(docId) | ||
console.log(JSON.stringify(doc.content, null, 2)) | ||
try { | ||
const doc = await ceramic.loadDocument(docId) | ||
console.log(JSON.stringify(doc.content, null, 2)) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
ceramic.close() | ||
}) | ||
|
||
|
@@ -40,8 +49,12 @@ program | |
.description('Show the state of a document') | ||
.action(async (docId) => { | ||
const ceramic = new CeramicClient() | ||
const doc = await ceramic.loadDocument(docId) | ||
console.log(JSON.stringify(doc.state, null, 2)) | ||
try { | ||
const doc = await ceramic.loadDocument(docId) | ||
console.log(JSON.stringify(doc.state, null, 2)) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
ceramic.close() | ||
}) | ||
|
||
|
@@ -50,12 +63,16 @@ program | |
.description('Watch for updates in a document') | ||
.action(async (docId) => { | ||
const ceramic = new CeramicClient() | ||
const doc = await ceramic.loadDocument(docId) | ||
console.log(JSON.stringify(doc.content, null, 2)) | ||
doc.on('change', () => { | ||
console.log('--- document changed ---') | ||
try { | ||
const doc = await ceramic.loadDocument(docId) | ||
console.log(JSON.stringify(doc.content, null, 2)) | ||
}) | ||
doc.on('change', () => { | ||
console.log('--- document changed ---') | ||
console.log(JSON.stringify(doc.content, null, 2)) | ||
}) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
}) | ||
|
||
program | ||
|
@@ -65,8 +82,12 @@ program | |
.action(async (docId, content) => { | ||
content = JSON.parse(content) | ||
const ceramic = new CeramicClient() | ||
const doc = await ceramic._updateDocument(docId, content) | ||
console.log(JSON.stringify(doc.id, null, 2)) | ||
try { | ||
const doc = await ceramic._updateDocument(docId, content) | ||
console.log(JSON.stringify(doc.content, null, 2)) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
ceramic.close() | ||
}) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's normalize the URL concatenation in order to avoid multiple slashes if the
apiUrl
already ends with/
. Maybe a separate utility function.