Skip to content

Commit 32350d8

Browse files
committed
added snippets to client readme intro
1 parent a748ca8 commit 32350d8

6 files changed

Lines changed: 91 additions & 156 deletions

File tree

packages/js/client/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/examples/**/*.d.ts
2+
/examples/**/*.js
3+
/examples/**/*.js.map

packages/js/client/README.md

Lines changed: 24 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -15,93 +15,42 @@ npm install --save @polywrap/client-js
1515

1616
## Usage
1717

18-
### Instantiate the client
18+
### Instantiate
19+
20+
Use the PolywrapClient [constructor](#constructor) to instantiate the client with the default configuration bundle.
21+
1922
```ts
20-
import { PolywrapClient } from "@polywrap/client-js";
23+
import { PolywrapClient } from "@polywrap/client-js";
2124

22-
const client = new PolywrapClient();
25+
const client = new PolywrapClient();
2326
```
2427

25-
### Invoke a wrapper
28+
### Configure
29+
30+
Use the `@polywrap/client-config-builder-js` package to build a custom configuration for your project.
2631

2732
```ts
28-
await client.invoke({
29-
uri: "ens/helloworld.dev.polywrap.eth",
30-
method: "logMessage",
31-
args: {
32-
message: "Hello World!"
33-
}
34-
});
33+
const config = new ClientConfigBuilder().addDefaults().buildCoreConfig();
34+
35+
const client = new PolywrapClient(config, { noDefaults: true });
3536
```
3637

37-
### Configure the client
38+
### Invoke
39+
40+
Invoke a wrapper.
3841

3942
```ts
40-
const config = {
41-
// redirect invocations from one uri to another
42-
redirects: [
43-
{
44-
from: "wrap://ens/from.eth",
45-
to: "wrap://ens/to.eth",
43+
const result = await client.invoke({
44+
uri: "ens/helloworld.dev.polywrap.eth",
45+
method: "logMessage",
46+
args: {
47+
message: "Hello World!"
4648
}
47-
],
48-
// add embedded Wasm wrappers
49-
wrappers: [
50-
{
51-
uri: "wrap://fs/simple/wrapper/uri/build",
52-
wrapper: WasmWrapper.from(
53-
fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info")),
54-
fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))
55-
)
56-
}
57-
],
58-
// add and configure embedded plugin wrappers
59-
packages: [
60-
{
61-
uri: "wrap://ens/ipfs.polywrap.eth",
62-
package: ipfsPlugin({}),
63-
},
64-
],
65-
// declare interface implementations
66-
interfaces: [
67-
{
68-
interface: "wrap://ens/uri-resolver.core.polywrap.eth",
69-
implementations: [
70-
"wrap://ens/ipfs-resolver.polywrap.eth",
71-
],
72-
},
73-
],
74-
// set environmental variables for a wrapper
75-
envs: [
76-
{
77-
uri: "wrap://ens/ipfs.polywrap.eth",
78-
env: {
79-
provider: "https://ipfs.wrappers.io",
80-
},
81-
},
82-
],
83-
// customize URI resolution
84-
resolvers: [
85-
new RecursiveResolver(
86-
new PackageToWrapperCacheResolver(wrapperCache, [
87-
new ExtendableUriResolver(),
88-
])
89-
)
90-
],
91-
92-
// custom wrapper cache
93-
wrapperCache: new WrapperCache(),
94-
95-
// tracer configuration - see @polywrap/tracing-js package
96-
tracerConfig: { ... },
97-
};
98-
```
99-
```ts
100-
// create a client by modifying the default configuration bundle
101-
const client = new PolywrapClient(config);
49+
});
50+
51+
if (!result.ok) throw result.error;
10252

103-
// or remove and replace the default configuration
104-
const altClient = new PolywrapClient(config, { noDefaults: true });
53+
const value = result.value;
10554
```
10655

10756
# Reference
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { ClientConfigBuilder, PolywrapClient } from "../build";
2+
3+
export function instantiate(): PolywrapClient {
4+
// /* $: quickstart-instantiate */ import { PolywrapClient } from "@polywrap/client-js";
5+
6+
const client = new PolywrapClient();
7+
// $end
8+
9+
return client;
10+
}
11+
12+
export function configure(): PolywrapClient {
13+
// $start: quickstart-configure
14+
const config = new ClientConfigBuilder().addDefaults().buildCoreConfig();
15+
16+
const client = new PolywrapClient(config, { noDefaults: true });
17+
// $end
18+
19+
return client;
20+
}
21+
22+
export async function invoke(): Promise<any> {
23+
const client = new PolywrapClient();
24+
// $start: quickstart-invoke
25+
const result = await client.invoke({
26+
uri: "ens/helloworld.dev.polywrap.eth",
27+
method: "logMessage",
28+
args: {
29+
message: "Hello World!"
30+
}
31+
});
32+
33+
if (!result.ok) throw result.error;
34+
35+
const value = result.value;
36+
// $end
37+
return value;
38+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"include": [
4+
"./**/*.ts"
5+
],
6+
}

packages/js/client/package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
"build"
1313
],
1414
"scripts": {
15-
"build": "yarn build:fast && yarn build:readme",
15+
"build": "yarn build:fast && yarn build:snippets && yarn build:readme",
1616
"build:fast": "rimraf ./build && tsc --project tsconfig.build.json",
1717
"lint": "eslint --color -c ../../../.eslintrc.js src/",
1818
"test": "jest --passWithNoTests --runInBand --verbose=true --detectOpenHandles --forceExit",
1919
"test:ci": "jest --passWithNoTests --runInBand --verbose --detectOpenHandles --forceExit",
2020
"test:rust": "jest --passWithNoTests --runInBand --verbose --detectOpenHandles --forceExit --config ./jest.rs.config.js",
2121
"test:watch": "jest --watch --passWithNoTests --verbose --detectOpenHandles",
22+
"build:snippets": "tsc --project ./examples/tsconfig.examples.json",
2223
"build:readme": "yarn doc-snippets combine"
2324
},
2425
"dependencies": {
@@ -65,13 +66,16 @@
6566
},
6667
"doc-snippets": {
6768
"extract": {
68-
"include": "./**/*.{ts,tsx,json,yaml,txt,md,graphql,cue}",
69+
"include": [
70+
"./src/**/*.{ts,tsx,json,yaml,txt,md,graphql,cue}",
71+
"./examples/**/*.{ts,tsx,json,yaml,txt,md,graphql,cue}"
72+
],
6973
"ignore": [
7074
"./**/node_modules/**",
7175
"./**/.polywrap/**",
7276
"./**/__tests__/**"
7377
],
74-
"dir": "./src"
78+
"dir": "./"
7579
},
7680
"inject": {
7781
"dir": "./readme",

packages/js/client/readme/README.md

Lines changed: 13 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -15,93 +15,28 @@ npm install --save @polywrap/client-js
1515

1616
## Usage
1717

18-
### Instantiate the client
19-
```ts
20-
import { PolywrapClient } from "@polywrap/client-js";
21-
22-
const client = new PolywrapClient();
23-
```
18+
### Instantiate
2419

25-
### Invoke a wrapper
20+
Use the PolywrapClient [constructor](#constructor) to instantiate the client with the default configuration bundle.
2621

2722
```ts
28-
await client.invoke({
29-
uri: "ens/helloworld.dev.polywrap.eth",
30-
method: "logMessage",
31-
args: {
32-
message: "Hello World!"
33-
}
34-
});
23+
$snippet: quickstart-instantiate
3524
```
3625

37-
### Configure the client
26+
### Configure
27+
28+
Use the `@polywrap/client-config-builder-js` package to build a custom configuration for your project.
3829

3930
```ts
40-
const config = {
41-
// redirect invocations from one uri to another
42-
redirects: [
43-
{
44-
from: "wrap://ens/from.eth",
45-
to: "wrap://ens/to.eth",
46-
}
47-
],
48-
// add embedded Wasm wrappers
49-
wrappers: [
50-
{
51-
uri: "wrap://fs/simple/wrapper/uri/build",
52-
wrapper: WasmWrapper.from(
53-
fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.info")),
54-
fs.readFileSync(path.join(simpleWrapperPath, "build/wrap.wasm"))
55-
)
56-
}
57-
],
58-
// add and configure embedded plugin wrappers
59-
packages: [
60-
{
61-
uri: "wrap://ens/ipfs.polywrap.eth",
62-
package: ipfsPlugin({}),
63-
},
64-
],
65-
// declare interface implementations
66-
interfaces: [
67-
{
68-
interface: "wrap://ens/uri-resolver.core.polywrap.eth",
69-
implementations: [
70-
"wrap://ens/ipfs-resolver.polywrap.eth",
71-
],
72-
},
73-
],
74-
// set environmental variables for a wrapper
75-
envs: [
76-
{
77-
uri: "wrap://ens/ipfs.polywrap.eth",
78-
env: {
79-
provider: "https://ipfs.wrappers.io",
80-
},
81-
},
82-
],
83-
// customize URI resolution
84-
resolvers: [
85-
new RecursiveResolver(
86-
new PackageToWrapperCacheResolver(wrapperCache, [
87-
new ExtendableUriResolver(),
88-
])
89-
)
90-
],
91-
92-
// custom wrapper cache
93-
wrapperCache: new WrapperCache(),
94-
95-
// tracer configuration - see @polywrap/tracing-js package
96-
tracerConfig: { ... },
97-
};
31+
$snippet: quickstart-configure
9832
```
99-
```ts
100-
// create a client by modifying the default configuration bundle
101-
const client = new PolywrapClient(config);
10233

103-
// or remove and replace the default configuration
104-
const altClient = new PolywrapClient(config, { noDefaults: true });
34+
### Invoke
35+
36+
Invoke a wrapper.
37+
38+
```ts
39+
$snippet: quickstart-invoke
10540
```
10641

10742
# Reference

0 commit comments

Comments
 (0)