mirror of
https://github.com/fluencelabs/examples
synced 2025-06-23 14:51:33 +00:00
feat!: Replace old fluence-js with JS Client (#425)
This commit is contained in:
30
js-client-examples/hello-world/.gitignore
vendored
Normal file
30
js-client-examples/hello-world/.gitignore
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
/dist
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
|
||||
# fluence
|
||||
|
||||
src/_aqua/*
|
8
js-client-examples/hello-world/.prettierrc.cjs
Normal file
8
js-client-examples/hello-world/.prettierrc.cjs
Normal file
@ -0,0 +1,8 @@
|
||||
module.exports = {
|
||||
semi: true,
|
||||
trailingComma: 'all',
|
||||
singleQuote: true,
|
||||
printWidth: 120,
|
||||
tabWidth: 4,
|
||||
useTabs: false,
|
||||
};
|
17
js-client-examples/hello-world/aqua/hello-world.aqua
Normal file
17
js-client-examples/hello-world/aqua/hello-world.aqua
Normal file
@ -0,0 +1,17 @@
|
||||
import Peer from "@fluencelabs/aqua-lib/builtin.aqua"
|
||||
|
||||
service HelloWorld("hello-world"):
|
||||
hello(str: string)
|
||||
getFortune() -> string
|
||||
|
||||
func sayHello():
|
||||
HelloWorld.hello("Hello, world!")
|
||||
|
||||
func tellFortune() -> string:
|
||||
res <- HelloWorld.getFortune()
|
||||
<- res
|
||||
|
||||
func getRelayTime() -> u64:
|
||||
on HOST_PEER_ID:
|
||||
ts <- Peer.timestamp_ms()
|
||||
<- ts
|
16
js-client-examples/hello-world/jest.config.cjs
Normal file
16
js-client-examples/hello-world/jest.config.cjs
Normal file
@ -0,0 +1,16 @@
|
||||
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
||||
module.exports = {
|
||||
extensionsToTreatAsEsm: ['.ts'],
|
||||
moduleNameMapper: {
|
||||
'^(\\.{1,2}/.*)\\.js$': '$1',
|
||||
},
|
||||
testPathIgnorePatterns: ['dist'],
|
||||
transform: {
|
||||
'^.+\\.tsx?$': [
|
||||
'ts-jest',
|
||||
{
|
||||
useESM: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
48867
js-client-examples/hello-world/package-lock.json
generated
Normal file
48867
js-client-examples/hello-world/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
32
js-client-examples/hello-world/package.json
Normal file
32
js-client-examples/hello-world/package.json
Normal file
@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "hello-world",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"type": "module",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"prestart": "npm run compile-aqua",
|
||||
"prebuild": "npm run compile-aqua",
|
||||
"start": "node --loader ts-node/esm ./src/index.ts",
|
||||
"build": "tsc",
|
||||
"test": "jest",
|
||||
"compile-aqua": "fluence aqua -i ./aqua/ -o ./src/_aqua",
|
||||
"watch-aqua": "fluence aqua -w -i ./aqua/ -o ./src/_aqua"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@fluencelabs/cli": "0.2.41",
|
||||
"@fluencelabs/aqua-lib": "0.6.0",
|
||||
"ts-node": "10.9.1",
|
||||
"typescript": "^4.6.3",
|
||||
"@types/jest": "29.4.0",
|
||||
"jest": "29.4.1",
|
||||
"ts-jest": "29.0.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@fluencelabs/js-client.api": "0.11.2",
|
||||
"@fluencelabs/js-client.node": "0.6.4",
|
||||
"@fluencelabs/fluence-network-environment": "1.0.14"
|
||||
}
|
||||
}
|
14
js-client-examples/hello-world/src/__test__/test.spec.ts
Normal file
14
js-client-examples/hello-world/src/__test__/test.spec.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { main } from '../main.js';
|
||||
|
||||
describe('smoke test', () => {
|
||||
it('should work', async () => {
|
||||
console.log = jest.fn();
|
||||
|
||||
await main();
|
||||
|
||||
expect(console.log).toBeCalledTimes(3);
|
||||
expect(console.log).toHaveBeenNthCalledWith(1, 'Hello, world!');
|
||||
expect(console.log).toHaveBeenNthCalledWith(2, 'Wealth awaits you very soon.');
|
||||
expect(console.log).toHaveBeenNthCalledWith(3, 'The relay time is: ', expect.anything());
|
||||
}, 15000);
|
||||
});
|
3
js-client-examples/hello-world/src/index.ts
Normal file
3
js-client-examples/hello-world/src/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { main } from './main.js';
|
||||
|
||||
main().catch((e) => console.error('error: ', e));
|
30
js-client-examples/hello-world/src/main.ts
Normal file
30
js-client-examples/hello-world/src/main.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import '@fluencelabs/js-client.node';
|
||||
import { Fluence } from '@fluencelabs/js-client.api';
|
||||
import { randomKras } from '@fluencelabs/fluence-network-environment';
|
||||
import { registerHelloWorld, sayHello, getRelayTime, tellFortune } from './_aqua/hello-world.js';
|
||||
|
||||
export async function main() {
|
||||
await Fluence.connect(randomKras());
|
||||
|
||||
registerHelloWorld({
|
||||
hello: (str) => {
|
||||
console.log(str);
|
||||
},
|
||||
getFortune: async () => {
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(resolve, 1000);
|
||||
});
|
||||
return 'Wealth awaits you very soon.';
|
||||
},
|
||||
});
|
||||
|
||||
await sayHello();
|
||||
|
||||
console.log(await tellFortune());
|
||||
|
||||
const relayTime = await getRelayTime();
|
||||
|
||||
console.log('The relay time is: ', new Date(relayTime).toLocaleString());
|
||||
|
||||
await Fluence.disconnect();
|
||||
}
|
18
js-client-examples/hello-world/tsconfig.json
Normal file
18
js-client-examples/hello-world/tsconfig.json
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": ["es2015", "dom"],
|
||||
"outDir": "./dist/",
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"declaration": true,
|
||||
"declarationMap": false,
|
||||
"sourceMap": true,
|
||||
"moduleResolution": "nodenext"
|
||||
},
|
||||
"exclude": ["node_modules", "dist"],
|
||||
"include": ["src"]
|
||||
}
|
Reference in New Issue
Block a user