Skip to content
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

+ add decentralized-fs module that support save and find like db orm #1

Merged
merged 17 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ typings/

# Nuxt.js build / generate output
.nuxt
dist
dist/
lib/

# Gatsby files
.cache/
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# typescript-common
Typescript common util library for both Frontend and Backend usage

### Installation

1. Install yarn globally (needed to resolve dependencies correctly when working in a monorepo)

```shell
npm install -g yarn
```

2. Install NPM packages

```shell
yarn install
```

3. Run all the examples

```shell
yarn run test
```

4. before run ipfs daemon
```
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST", "OPTIONS"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials '["true"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Headers '["Authorization"]'
ipfs config --json API.HTTPHeaders.Access-Control-Expose-Headers '["Location"]'
ipfs daemon
```
32 changes: 32 additions & 0 deletions build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"compilerOptions": {
"composite": true,
"target": "es6",
"allowJs": false,
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"declaration": true,
"declarationMap": true,
"removeComments": false,
"strict": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"lib": ["es2018", "esnext.asynciterable", "DOM"],
"baseUrl": "."
},
"exclude": [
"node_modules",
"**/__mocks__/*",
"**/__tests__/*",
"**/*.spec.ts",
"**/*.test.ts"
]
}
63 changes: 63 additions & 0 deletions configure-references.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env node

// @ts-check
/* eslint-disable */

const fs = require('fs');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const path = require('path');
const isCI = require('is-ci');

const config = JSON.parse(fs.readFileSync('tsconfig.json').toString());
config.files = [];
config.references = [];

(async function() {
if (isCI) {
// dont run it on CI
return;
}

const { stdout, stderr } = await exec('yarn workspaces info --json');

const lines = stdout.split('\n');
const depthTree = lines.slice(1, lines.length - 2).join('\n');
const workspaces = JSON.parse(depthTree);

for (const name in workspaces) {
const workspace = workspaces[name];
const location = path.resolve(process.cwd(), workspace.location);
const tsconfigPath = path.resolve(location, 'tsconfig.json');
if (fs.existsSync(tsconfigPath)) {
config.references.push({
path: workspace.location,
});
const workspaceConfig = JSON.parse(
fs.readFileSync(tsconfigPath).toString(),
);
workspaceConfig.compilerOptions.composite = true;
workspaceConfig.references = [];
for (const dependency of workspace.workspaceDependencies) {
const dependecyLocation = path.resolve(
process.cwd(),
workspaces[dependency].location,
);
if (
fs.existsSync(
path.resolve(dependecyLocation, 'tsconfig.json'),
)
) {
workspaceConfig.references.push({
path: path.relative(location, dependecyLocation),
});
}
}
fs.writeFileSync(
tsconfigPath,
JSON.stringify(workspaceConfig, undefined, 4),
);
}
}
fs.writeFileSync('tsconfig.json', JSON.stringify(config, undefined, 4));
})();
3 changes: 3 additions & 0 deletions examples/browser-nextjs/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
34 changes: 34 additions & 0 deletions examples/browser-nextjs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel
34 changes: 34 additions & 0 deletions examples/browser-nextjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `pages/index.tsx`. The page auto-updates as you edit the file.

[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.ts`.

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
83 changes: 83 additions & 0 deletions examples/browser-nextjs/components/ipfs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { useState, useEffect } from "react";
import DecentralizedFileStorage from "@konomi/decentralized-fs/dist/decentralized-fs";

const IpfsComponent = () => {
const [id, setId] = useState(null);
const [ipfs, setIpfs] = useState(null);
const [version, setVersion] = useState(null);
const [isOnline, setIsOnline] = useState(false);
const [cid, setCID] = useState(null);
const [content, setContent] = useState(null);

useEffect(() => {
const init = async () => {
if (ipfs) return;

const dfs = new DecentralizedFileStorage("http://localhost:5002");

const dfsId = await dfs.id();
const dfsVersion = await dfs.version();
const dfsIsOnline = dfs.isOnline;

setIpfs(dfs);
setId(dfsId.id);
setVersion(dfsVersion.version);
setIsOnline(dfsIsOnline);

const mockData = {
symbol: "kono",
slug: "konomi",
client: 0,
aggregationStrategy: 1,
sources: [
{
type: 3, // for uniswap
detail: {
address: "0x...",
},
},
{
type: 2, // coinmarcketcap
detail: {
coinId: "2",
},
},
],
};

const cid = await dfs.save(JSON.stringify(mockData));
setCID(cid);

const content = await dfs.find(cid);
setContent(Uint8ArrayToString(content.split(",")));
};

init();
}, [ipfs]);

const Uint8ArrayToString = (u8aStr: number[]) => {
var dataString = "";
for (const v of u8aStr) {
dataString += String.fromCharCode(v);
}
return dataString;
};

if (!ipfs) {
return "<h4>Connecting to IPFS...</h4>";
}

return (
<div className="hello">
<div className="greeting">
<h4 data-test="id">Id: {id}</h4>
<h4 data-test="version">Version: {version}</h4>
<h4 data-test="status">Status: {isOnline ? "Online" : "Offline"}</h4>
<h4 data-test="cid">CID: {cid}</h4>
<h4 data-test="content">content: {content}</h4>
</div>
</div>
);
};

export default IpfsComponent;
6 changes: 6 additions & 0 deletions examples/browser-nextjs/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
10 changes: 10 additions & 0 deletions examples/browser-nextjs/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('next').NextConfig} */
module.exports = {
reactStrictMode: true,
// https://github.com/vercel/next.js/issues/21079
// Remove the workaround the issue is fixed
images: {
loader: "imgix",
path: "",
}
}
Loading