Skip to content

Commit

Permalink
docs: update docs for blocklet sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
linchen1987 committed Jun 9, 2023
1 parent 91cf281 commit 78d8e65
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 123 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.130 (June 09, 2023)

- docs: update docs for blocklet sdk

## 0.1.129 (April 11, 2023)

- docs: polish image
Expand Down
2 changes: 1 addition & 1 deletion developer/docs/blocklet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repository:
type: git
url: git+https://github.com/blocklet/blocklet-site.git
specVersion: 1.2.8
version: 0.1.129
version: 0.1.130
logo: logo.png
files:
- hooks/post-start.js
Expand Down
103 changes: 44 additions & 59 deletions developer/docs/pages/reference/blocklet-sdk/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,20 +470,42 @@ import { Database } from '@blocklet/sdk';
import { env } from '@blocklet/sdk';

const {
appId, // the id of the app
appId, // the did of the app
appPid, // the permenant did of the app
appIds, // all did's that the application has previously used
appName, // the title of the app, used to display to user
appDescription, // the description of the app
appUrl, // the web url of the app
isComponent, // the blocklet is running as an app or a component
dataDir, // the data dir of the blocklet
cacheDir, // the cache dir of the blocklet
mode, // in which mode the blocklet is running
appStorageEndpoint // the endpoint of the DID Spaces of the app
serverVersion: // the version of the server where the app is running
preferences, // blocklet preferences. default: {}
} = env;
```

Please reference [Blocklet Preferences](/how-to/preferences) for how to change the structure and value in `env.preferences`.

## Config

Unlike Environment, the information in Config will be updated in real time, and the application does not need to be restarted

```javascript
import { env, components } from '@blocklet/config'
```

- `env` same as env in Environment
- `components` **Array\<object\>**
- `title` component title
- `did` component did
- `name` component name
- `mountPoint` e.g. '/', '/blog'
- `status` **import(@blocklet/constant).BlockletStatus**
- `port` e.g. 5678
- `webEndpoint` e.g. http://127.0.0.1:5678

### mode

In which mode the blocklet is running
Expand All @@ -499,38 +521,11 @@ env.mode === 'production'; // The blocklet is running in the production mode
import { Component } from '@blocklet/sdk';
```

### getParentWebEndpoint

`Component.getParentWebEndpoint()`

- _@return_ endpoint of parent component. e.g. `http://127.0.0.1:5678`

### getChildWebEndpoint

`Component.getChildWebEndpoint(name)`

- _@param_ **name** `string` the name of **component instance** defined in parent's blocklet.yml

If blocklet.yml is

```
components
- name: component-1
source:
store: xxx
name: xxx
version: xxx
```

the blocklet should use like this: `Component.getChildWebEndpoint('component-1')`

- _@return_ endpoint of child component. e.g. `http://127.0.0.1:5678`

### getComponentWebEndpoint

`Component.getComponentWebEndpoint(name)`

Get endpoint of first-level component of app
Get endpoint of component of app

- _@param_ **name** `string` the name or title or did of the **component bundle**

Expand All @@ -556,7 +551,7 @@ Get endpoint of first-level component of app

`Component.getComponentMountPoint(name)`

Get mount point of first-level component of app
Get mount point of component of app

- _@param_ **name** `string` the name or title or did of the **component bundle**

Expand All @@ -580,79 +575,69 @@ Get mount point of first-level component of app

### call

Communicate with child component or parent component safely
Communicate with component component safely

`Component.call({ name, path, data })`

- _@param_ **name** `string`
- parent call child by set `name` to **component instance** defined in parent's blocklet.yml
- parent call child by set `name` to empty
- _@param_ **name** `string` the name or title or did of the **component bundle**
- _@param_ **path** `string` the http api. e.g. `/api/xxx`
- blocklet must use **POST** for the api
- _@param_ **data** `object` the payload
- _@param_ **method** `object` http method
- _@param_ **responseType** `undefined | 'stream'` response type
- _@return_ `object` the response of axios https://github.com/axios/axios#response-schema

e.g.

parent:

```yml
components:
- name: component1
source:
store: xxx
name: bundle-component-1
version: xxx
```
component-1:

```js
import { Component, middlewares } from '@blocklet/sdk';

const app = express();

app.post(
'/api/parent',
'/api/component-2',

// You should use verifySig middleware to prevent unknown request
middlewares.component.verifySig,

(req, res) => {
// req.body is { msg: "ping from child" } if the request is from component1
// req.body is { msg: "ping from component-2" } if the request is from component-2

res.json({ msg: 'pong from parent' });
res.json({ msg: 'pong from component-1' });
}
);

// data: { msg: 'pong from child' }
// data: { msg: 'pong from component-2' }
const { data } = await Component.call({
name: 'component1',
path: '/api/child',
data: { msg: 'ping from parent' },
name: 'component-1',
path: '/api/component-2',
data: { msg: 'ping from component-1' },
});
```

bundle-component-1:
component-2:

```js
const app = express();

app.post(
'/api/child',
'/api/component-2',

// You should use verifySig middleware to prevent unknown request
middlewares.component.verifySig,

(req, res) => {
// req.body is { msg: "ping from parent" } if the request is from parent
// req.body is { msg: "ping from component-1" } if the request is from component-1

res.json({ msg: 'pong from child' });
res.json({ msg: 'pong from component-2' });
}
);

// data: { msg: 'pong from parent' }
// data: { msg: 'pong from component-1' }
const { data } = await Component.call({
path: '/api/parent',
data: 'ping from child',
path: '/api/component-1',
data: 'ping from component-2',
});
```

Expand Down

0 comments on commit 78d8e65

Please sign in to comment.