Skip to content

Commit

Permalink
add example-mina
Browse files Browse the repository at this point in the history
  • Loading branch information
wangcheng committed Oct 30, 2023
1 parent 74d2832 commit 616ec7f
Show file tree
Hide file tree
Showing 14 changed files with 410 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"private": true,
"workspaces": [
"packages/connect-miniprogram",
"packages/example-taro"
"packages/example-taro",
"packages/example-mina"
],
"prettier": {
"arrowParens": "always",
Expand Down
2 changes: 2 additions & 0 deletions packages/example-mina/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
miniprogram/project.private.config.json
miniprogram/generated_modules
2 changes: 2 additions & 0 deletions packages/example-mina/miniprogram/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// app.js
App({});
19 changes: 19 additions & 0 deletions packages/example-mina/miniprogram/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"pages": ["pages/index/index"],
"window": {
"navigationBarTextStyle": "black",
"navigationStyle": "custom"
},
"style": "v2",
"renderer": "skyline",
"rendererOptions": {
"skyline": {
"defaultDisplayBlock": true,
"disableABTest": true,
"sdkVersionBegin": "3.0.0",
"sdkVersionEnd": "15.255.255"
}
},
"componentFramework": "glass-easel",
"lazyCodeLoading": "requiredComponents"
}
10 changes: 10 additions & 0 deletions packages/example-mina/miniprogram/app.wxss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**app.wxss**/
.container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
padding: 200rpx 0;
box-sizing: border-box;
}
7 changes: 7 additions & 0 deletions packages/example-mina/miniprogram/pages/index/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Page({
onReady: () => {
require.async('../../generated_modules/connect-client').then(({ main }) => {
main();
});
},
});
1 change: 1 addition & 0 deletions packages/example-mina/miniprogram/pages/index/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
6 changes: 6 additions & 0 deletions packages/example-mina/miniprogram/pages/index/index.wxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!--index.wxml-->
<scroll-view class="scrollarea" scroll-y type="list">
<view class="container">
Weixin
</view>
</scroll-view>
10 changes: 10 additions & 0 deletions packages/example-mina/miniprogram/pages/index/index.wxss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**index.wxss**/
page {
height: 100vh;
display: flex;
flex-direction: column;
}
.scrollarea {
flex: 1;
overflow-y: hidden;
}
29 changes: 29 additions & 0 deletions packages/example-mina/miniprogram/project.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"appid": "wx043e0b542b3515b8",
"compileType": "miniprogram",
"libVersion": "3.1.5",
"packOptions": {
"ignore": [],
"include": []
},
"setting": {
"urlCheck": false,
"coverView": true,
"es6": true,
"postcss": true,
"minified": true,
"enhance": true,
"showShadowRootInWxmlPanel": true,
"packNpmRelationList": [],
"babelSetting": {
"ignore": [],
"disablePlugins": [],
"outputPath": ""
}
},
"condition": {},
"editorSetting": {
"tabIndent": "insertSpaces",
"tabSize": 2
}
}
17 changes: 17 additions & 0 deletions packages/example-mina/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "example-mina",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "esbuild src/connect-client.js --bundle --outfile=miniprogram/generated_modules/connect-client.js --format=cjs --target=es2015 --inject:src/polyfill.js"
},
"dependencies": {
"@buf/connectrpc_eliza.connectrpc_es": "^1.1.3-20230913231627-233fca715f49.1",
"@bufbuild/protobuf": "^1.4.1",
"@connectrpc/connect": "^1.1.3",
"connect-miniprogram": "workspace:packages/connect-miniprogram"
},
"devDependencies": {
"esbuild": "^0.19.5"
}
}
52 changes: 52 additions & 0 deletions packages/example-mina/src/connect-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { createPromiseClient } from '@connectrpc/connect';
import {
createConnectTransport,
createGrpcWebTransport,
} from 'connect-miniprogram/src';
import { ElizaService } from '@buf/connectrpc_eliza.connectrpc_es/connectrpc/eliza/v1/eliza_connect';

export const isDevTool = wx.getSystemInfoSync().platform === 'devtools';

export const baseUrl = 'https://demo.connectrpc.com';

const grpcTransport = createGrpcWebTransport({
baseUrl,
request: wx.request,
isDevTool,
});

const grpcClient = createPromiseClient(ElizaService, grpcTransport);

const connectTransport = createConnectTransport({
baseUrl,
request: wx.request,
isDevTool,
});

const connectClient = createPromiseClient(ElizaService, connectTransport);

export async function main() {
connectClient
.say({
sentence: 'I feel happy.',
})
.then((res) => {
console.log('[connect.say]', res);
});

for await (const res of connectClient.introduce({ name: 'Joseph' })) {
console.log('[connect.introduce]', res);
}

grpcClient
.say({
sentence: 'I feel happy.',
})
.then((res) => {
console.log('[grpc.say]', res);
});

for await (const res of grpcClient.introduce({ name: 'Joseph' })) {
console.log('[grpc.introduce]', res);
}
}
11 changes: 11 additions & 0 deletions packages/example-mina/src/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {
HeadersPolyfill,
FastTextDecoder,
FastTextEncoder,
} from 'connect-miniprogram/polyfill';

export {
HeadersPolyfill as Headers,
FastTextDecoder as TextDecoder,
FastTextEncoder as TextEncoder,
};
Loading

0 comments on commit 616ec7f

Please sign in to comment.