├── .babelrc ├── .gitignore ├── .npmignore ├── .prettierrc ├── LICENSE ├── README.md ├── dist ├── index.js └── index.js.map ├── package.json ├── src ├── CloudFile.js ├── DatabaseQuery.js ├── DatabaseWatch.js ├── LoginState.js ├── UploadCloudFile.js └── index.js ├── test └── DatabaseWatch.test.js ├── vue-playground ├── .eslintignore ├── .eslintrc.js ├── .gitignore ├── README.md ├── babel.config.js ├── package.json ├── public │ ├── favicon.ico │ └── index.html ├── src │ ├── App.vue │ ├── assets │ │ └── logo.png │ ├── components │ │ └── HelloWorld.vue │ └── main.js └── vue.config.js └── webpack.config.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "@babel/preset-env" 4 | ], 5 | "env": { 6 | "test": { 7 | "plugins": [ 8 | "@babel/plugin-transform-runtime" 9 | ] 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | 63 | yarn.lock 64 | package-lock.json -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | vue-playground 2 | test -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "printWidth": 100, 3 | "parser": "flow", 4 | "semi": false, 5 | "tabWidth": 4 6 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 云开发 Vue3插件 2 | 3 | [云开发 Vue 插件](https://github.com/TencentCloudBase/cloudbase-vue) 是云开发官方维护的 Vue 插件,提供全局入口、Vue 逻辑组件等功能。 4 | 5 | ## 安装 6 | 7 | ``` 8 | npm install --save cloudbase-vue-next 9 | ``` 10 | 11 | ------ 12 | 13 | ## 使用 14 | 15 | 下面我们使用 `LoginState` 组件,来动态绑定当前页面的登录态。 16 | 17 | - 页面初始化时,显示 `'未登录'` 18 | - 之后我们调用[匿名登录](https://docs.cloudbase.net/authentication/ 19 | anonymous.html),如果登录成功,则文案将变成 `'已登录'` 20 | 21 | main.js 22 | ```js 23 | import {createApp} from "vue" 24 | import Cloudbase from "cloudbase-vue-next" 25 | import App from "./App.vue" 26 | const app = createApp(App) 27 | app.use(Cloudbase, { 28 | env: "your-env-id", 29 | region: "your-env-region" 30 | }) 31 | app.mount('#app'); 32 | ``` 33 | 34 | App.vue 35 | ```html 36 | 45 | 46 | 70 | ``` 71 | 在setup中使用 72 | ```html 73 | 82 | 83 | 113 | ``` 114 | -------- 115 | 116 | ## Plugin API 117 | 118 | ### Vue.$cloudbase 119 | 120 | 可以在 Vue 组件的 `this.$cloudbase` 中,获取 Cloudbase 实例 121 | ```js 122 | export default { 123 | data() { 124 | return { 125 | docs: [] 126 | } 127 | }, 128 | async created() { 129 | // 登录 130 | await this.$cloudbase.auth({ persistence: "local" }).signInWithTicket(ticket) 131 | // 数据库查询 132 | const queryResult = await this.$cloudbase.database().collection('test').where({}).get() 133 | this.docs = queryResult.data 134 | } 135 | } 136 | ``` 137 | -------- 138 | 139 | ## Components 140 | 141 | | 组件 | 功能 | 142 | | ---- | ---- | 143 | | [LoginState](#loginstate) | 获取并绑定登录状态 | 144 | | [DatabaseQuery](#databasequery) | 数据库查询 | 145 | | [DatabaseWatch](#databasewatch) | 数据库实时推送 | 146 | | [CloudFile](#cloudfile) | 获取云存储中的文件 | 147 | 148 | ### LoginState 149 | 获取登录状态 150 | 151 | #### Props 152 | 153 | 暂无 154 | 155 | #### Slots 156 | 157 | | slot | type | 描述 | 158 | | ---------- | ---------------------- | ------------ | 159 | | loginState | `null` or `LoginState` | 当前是否登录 | 160 | | loading | `boolean` | 是否加载中 | 161 | 162 | #### Example 163 | ```html 164 | 165 |

{{ loading ? '加载中' : (loginState ? '已登录' : '没登录') }}

166 |
167 | ``` 168 | 169 | ------- 170 | 171 | 172 | ### DatabaseQuery 173 | 174 | 数据库查询 175 | 176 | #### Props 177 | 178 | | prop | type | 描述 | 179 | | ---------- | ---------- | ------------------------------------------------ | 180 | | collection | `string` | 集合名 | 181 | | query | `function` | 返回自定的查询条件,如 `_ => ({ foo: _.gt(2) })` | 182 | 183 | #### Slot 184 | 185 | | slot | type | 描述 | 186 | | ------- | ----------------- | -------------- | 187 | | docs | `Array` | 文档组成的数组 | 188 | | loading | `boolean` | 是否加载中 | 189 | | error | `null` or `Error` | 错误 | 190 | 191 | #### Example 192 | ```html 193 | 198 |

199 | {{ text }} 200 |

201 |
202 | ``` 203 | 204 | ------- 205 | 206 | ### DatabaseWatch 207 | 208 | 数据库实时监听 209 | 210 | #### Props 211 | 212 | | prop | type | 描述 | 213 | | ---------- | ---------- | ------------------------------------------------ | 214 | | collection | `string` | 集合名 | 215 | | query | `function` | 返回自定的查询条件,如 `_ => ({ foo: _.gt(2) })` | 216 | 217 | #### Slot 218 | | slot | type | 描述 | 219 | | ------- | ----------------- | -------------- | 220 | | docs | `Array` | 文档组成的数组 | 221 | | loading | `boolean` | 是否加载中 | 222 | | error | `null` or `Error` | 错误 | 223 | 224 | #### Example 225 | ```html 226 | 231 |

232 | {{ text }} 233 |

234 |
235 | ``` 236 | 237 | ------ 238 | 239 | 240 | ### CloudFile 241 | 242 | 根据 `FileID`,获取云存储文件的真实 URL 243 | 244 | #### Props 245 | 246 | | slot | type | 描述 | 247 | | ---- | -------- | ----------------------------- | 248 | | id | `string` | 云存储 ID,形如 `cloud://...` | 249 | 250 | #### Slot 251 | | slot | type | 描述 | 252 | | ------- | ----------------- | -------------- | 253 | | url | `string` | 文件的真实 URL | 254 | | loading | `boolean` | 是否加载中 | 255 | | error | `null` or `Error` | 错误 | 256 | 257 | #### Example 258 | 259 | ```html 260 | 264 | {{ url ? url : 'loading...' }} 265 | 266 | ``` 267 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cloudbase-vue-next", 3 | "version": "0.1.3", 4 | "description": "", 5 | "main": "dist/index.js", 6 | "scripts": { 7 | "test": "jest", 8 | "dev": "webpack --watch", 9 | "build": "webpack" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/qiuquanwu/cloudbase-vue-next" 14 | }, 15 | "author": "qiuquanwu", 16 | "license": "ISC", 17 | "bugs": { 18 | "url": "https://github.com/qiuquanwu/cloudbase-vue-next/issues" 19 | }, 20 | "homepage": "https://github.com/qiuquanwu/cloudbase-vue-next#readme", 21 | "devDependencies": { 22 | "@babel/core": "^7.7.2", 23 | "@babel/plugin-transform-runtime": "^7.9.0", 24 | "@babel/preset-env": "^7.7.1", 25 | "@vue/test-utils": "^1.0.0-beta.32", 26 | "babel-loader": "^8.0.6", 27 | "cross-env": "^7.0.3", 28 | "css-loader": "^3.2.0", 29 | "jest": "^24.9.0", 30 | "less-loader": "^5.0.0", 31 | "optimize-css-assets-webpack-plugin": "^5.0.3", 32 | "vue-loader": "^15.7.2", 33 | "vue-template-compiler": "^2.6.10", 34 | "webpack": "4.41.2", 35 | "webpack-cli": "3.3.10", 36 | "vue": "^3.0.11" 37 | }, 38 | "dependencies": { 39 | "@cloudbase/js-sdk": "^1.4.0", 40 | "global": "^4.4.0", 41 | "lodash.isequal": "^4.5.0" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/CloudFile.js: -------------------------------------------------------------------------------- 1 | import { h } from "vue" 2 | export default { 3 | props: { 4 | tag: { 5 | type: String 6 | }, 7 | id: { 8 | required: true, 9 | type: String 10 | } 11 | }, 12 | data() { 13 | return { 14 | url: null, 15 | loading: true, 16 | error: false 17 | } 18 | }, 19 | async created() { 20 | const result = await this.$cloudbase.getTempFileURL({ 21 | fileList: [this.id] 22 | }) 23 | 24 | this.loading = false 25 | this.url = result.fileList[0].tempFileURL 26 | }, 27 | render() { 28 | const tag = this.tag || "div" 29 | let result = this.$slots.default 30 | ? this.$slots.default({ 31 | url: this.url, 32 | loading: this.loading 33 | }) 34 | : h(tag) 35 | if (Array.isArray(result)) { 36 | return h(tag, result) 37 | } else { 38 | return result 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/DatabaseQuery.js: -------------------------------------------------------------------------------- 1 | import isEqual from "lodash.isequal" 2 | import { h } from "vue" 3 | export default { 4 | props: { 5 | tag: { 6 | type: String 7 | }, 8 | collection: { 9 | required: true, 10 | type: String 11 | }, 12 | query: { 13 | type: Function 14 | } 15 | }, 16 | data() { 17 | return { 18 | docs: [], 19 | loading: true, 20 | error: null 21 | } 22 | }, 23 | async created() { 24 | return this.fetchData() 25 | }, 26 | watch: { 27 | collection() { 28 | return this.fetchData() 29 | }, 30 | queryObject(newValue, oldValue) { 31 | if (isEqual(newValue, oldValue)) { 32 | return 33 | } 34 | return this.fetchData() 35 | } 36 | }, 37 | computed: { 38 | queryObject() { 39 | const db = this.$cloudbase.database() 40 | const query = this.query ? this.query(db.command) : {} 41 | return query 42 | } 43 | }, 44 | methods: { 45 | async fetchData() { 46 | try { 47 | const db = this.$cloudbase.database() 48 | const res = await db 49 | .collection(this.collection) 50 | .where(this.queryObject) 51 | .get() 52 | this.docs = res.data 53 | this.loading = false 54 | } catch (e) { 55 | this.error = e 56 | } 57 | } 58 | }, 59 | render() { 60 | const tag = this.tag || "div" 61 | let result = this.$slots.default 62 | ? this.$slots.default({ 63 | docs: this.docs, 64 | loading: this.loading, 65 | error: this.error 66 | }) 67 | : h(tag) 68 | if (Array.isArray(result)) { 69 | return h(tag, result) 70 | } else { 71 | return result 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/DatabaseWatch.js: -------------------------------------------------------------------------------- 1 | import { h } from "vue" 2 | export default { 3 | props: { 4 | tag: { 5 | type: String 6 | }, 7 | collection: { 8 | required: true, 9 | type: String 10 | }, 11 | query: { 12 | type: Function 13 | } 14 | }, 15 | data() { 16 | return { 17 | docs: [], 18 | loading: true, 19 | error: false 20 | } 21 | }, 22 | async created() { 23 | const db = this.$cloudbase.database() 24 | const query = this.query ? this.query(db.command) : {} 25 | db.collection(this.collection) 26 | .where(query) 27 | .watch({ 28 | onChange: snapshot => { 29 | this.loading = false 30 | this.docs = snapshot.docs 31 | }, 32 | onError: () => {} 33 | }) 34 | }, 35 | render() { 36 | const tag = this.tag || "div" 37 | let result = this.$slots.default 38 | ? this.$slots.default({ 39 | docs: this.docs, 40 | loading: this.loading 41 | }) 42 | : h(tag) 43 | if (Array.isArray(result)) { 44 | return h(tag, result) 45 | } else { 46 | return result 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/LoginState.js: -------------------------------------------------------------------------------- 1 | import { h } from "vue" 2 | export default { 3 | props: { 4 | tag: String 5 | }, 6 | data() { 7 | return { 8 | loginState: null, 9 | loading: true 10 | } 11 | }, 12 | async created() { 13 | const loginState = await this.$cloudbase.auth().getLoginState() 14 | this.loginState = loginState || null 15 | this.loading = false 16 | //this.$cloudbase.on("loginStateChanged", this.onLoginStateChanged) 17 | }, 18 | beforeUnmount() { 19 | //this.$cloudbase.off("loginStateChanged", this.onLoginStateChanged) 20 | }, 21 | methods: { 22 | async onLoginStateChanged() { 23 | const loginState = await this.$cloudbase.auth().getLoginState() 24 | this.loginState = loginState || null 25 | } 26 | }, 27 | render() { 28 | const tag = this.tag || "div" 29 | let result = this.$slots.default 30 | ? this.$slots.default({ 31 | loginState: this.loginState, 32 | loading: this.loading 33 | }) 34 | : h(tag) 35 | if (Array.isArray(result)) { 36 | return h(tag, result) 37 | } else { 38 | return result 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/UploadCloudFile.js: -------------------------------------------------------------------------------- 1 | import { h } from "vue" 2 | export default { 3 | props: { 4 | tag: { 5 | type: String 6 | } 7 | }, 8 | data() { 9 | return { 10 | progress: 0, 11 | success: false, 12 | error: null 13 | } 14 | }, 15 | async created() {}, 16 | methods: { 17 | async uploadFile(path, file) { 18 | this.$cloudbase.uploadFile({ 19 | cloudPath: path, 20 | filePath: file, 21 | onUploadProgress(progressEvent) { 22 | console.log(progressEvent) 23 | const percentCompleted = Math.round( 24 | (progressEvent.loaded / progressEvent.total) * 100 25 | ) 26 | this.progress = percentCompleted 27 | } 28 | }) 29 | } 30 | }, 31 | render() { 32 | const tag = this.tag || "div" 33 | let result = this.$slots.default 34 | ? this.$slots.default({ 35 | upload: this.uploadFile, 36 | progress: this.progress, 37 | success: this.success 38 | }) 39 | : h(tag) 40 | if (Array.isArray(result)) { 41 | return h(tag, result) 42 | } else { 43 | return result 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import cloudbase from "@cloudbase/js-sdk" 2 | import LoginState from "./LoginState" 3 | import DatabaseWatch from "./DatabaseWatch" 4 | import CloudFile from "./CloudFile" 5 | import DatabaseQuery from "./DatabaseQuery" 6 | import { inject } from "vue" 7 | // import UploadCloudFile from './UploadCloudFile' 8 | 9 | const useCloud = () => { 10 | return inject("cloud") 11 | } 12 | const plugin = { 13 | install(app, options) { 14 | // Vue.component("databaseQuery", Query) 15 | app.component("LoginState", LoginState) 16 | app.component("DatabaseWatch", DatabaseWatch) 17 | app.component("CloudFile", CloudFile) 18 | app.component("DatabaseQuery", DatabaseQuery) 19 | // Vue.component("UploadCloudFile", UploadCloudFile) 20 | let cloud = cloudbase.init({ 21 | env: options.env, 22 | region: options.region 23 | }) 24 | app.config.globalProperties.$cloudbase = cloud 25 | app.provide("cloud", cloud) 26 | } 27 | } 28 | 29 | export default plugin 30 | export { useCloud } 31 | -------------------------------------------------------------------------------- /test/DatabaseWatch.test.js: -------------------------------------------------------------------------------- 1 | import { mount, shallowMount } from '@vue/test-utils' 2 | import DatabaseWatch from '../src/DatabaseWatch' 3 | 4 | let mockWatch, mockWhere, mockCollection, mockCloudbase 5 | beforeEach(() => { 6 | mockWatch = jest.fn(() => { }) 7 | mockWhere = jest.fn(() => { 8 | return { 9 | watch: mockWatch 10 | } 11 | }) 12 | mockCollection = jest.fn(() => { 13 | return { 14 | where: mockWhere 15 | } 16 | }) 17 | mockCloudbase = { 18 | database() { 19 | return { 20 | collection: mockCollection, 21 | command: { 22 | gt() { 23 | return 'this is a query' 24 | } 25 | } 26 | } 27 | } 28 | } 29 | }) 30 | describe('DatabaseWatch', () => { 31 | it("能正常渲染子节点", () => { 32 | const wrapper = mount(DatabaseWatch, { 33 | mocks: { 34 | $cloudbase: mockCloudbase 35 | }, 36 | propsData: { 37 | collection: 'test' 38 | }, 39 | slots: { 40 | default: '

ffffffffff

' 41 | } 42 | }) 43 | expect(wrapper.html()).toBe('
\n

ffffffffff

\n
') 44 | }) 45 | 46 | it("支持自定义tag", () => { 47 | const wrapper = mount(DatabaseWatch, { 48 | mocks: { 49 | $cloudbase: mockCloudbase 50 | }, 51 | propsData: { 52 | collection: 'test', 53 | tag: 'p' 54 | }, 55 | slots: { 56 | default: '

ffffffffff

' 57 | } 58 | }) 59 | expect(wrapper.html()).toBe('

\n

ffffffffff

\n

') 60 | }) 61 | 62 | it("内部调用collection()、where()和watch()", () => { 63 | const wrapper = mount(DatabaseWatch, { 64 | mocks: { 65 | $cloudbase: mockCloudbase 66 | }, 67 | propsData: { 68 | collection: 'test', 69 | tag: 'p', 70 | query: _ => _.gt() 71 | }, 72 | slots: { 73 | default: '

ffffffffff

' 74 | } 75 | }) 76 | expect(mockWhere.mock.calls[0][0]).toBe('this is a query') 77 | expect(mockCollection.mock.calls[0][0]).toBe('test') 78 | expect(typeof mockWatch.mock.calls[0][0].onChange).toBe('function') 79 | expect(typeof mockWatch.mock.calls[0][0].onError).toBe('function') 80 | }) 81 | }) -------------------------------------------------------------------------------- /vue-playground/.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /vue-playground/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | "env": { 3 | "browser": true, 4 | "es6": true 5 | }, 6 | "extends": [ 7 | "eslint:recommended", 8 | "plugin:vue/essential" 9 | ], 10 | "globals": { 11 | "Atomics": "readonly", 12 | "SharedArrayBuffer": "readonly" 13 | }, 14 | "parserOptions": { 15 | "ecmaVersion": 2018, 16 | "sourceType": "module" 17 | }, 18 | "plugins": [ 19 | "vue" 20 | ], 21 | "rules": { 22 | 'no-console': 0 23 | } 24 | }; -------------------------------------------------------------------------------- /vue-playground/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /dist 4 | 5 | # local env files 6 | .env.local 7 | .env.*.local 8 | 9 | # Log files 10 | npm-debug.log* 11 | yarn-debug.log* 12 | yarn-error.log* 13 | 14 | # Editor directories and files 15 | .idea 16 | .vscode 17 | *.suo 18 | *.ntvs* 19 | *.njsproj 20 | *.sln 21 | *.sw? 22 | 23 | yarn.lock 24 | package-lock.json -------------------------------------------------------------------------------- /vue-playground/README.md: -------------------------------------------------------------------------------- 1 | # vue-playground 2 | 3 | ## Project setup 4 | ``` 5 | yarn install 6 | ``` 7 | 8 | ### Compiles and hot-reloads for development 9 | ``` 10 | yarn serve 11 | ``` 12 | 13 | ### Compiles and minifies for production 14 | ``` 15 | yarn build 16 | ``` 17 | 18 | ### Lints and fixes files 19 | ``` 20 | yarn lint 21 | ``` 22 | 23 | ### Customize configuration 24 | See [Configuration Reference](https://cli.vuejs.org/config/). 25 | -------------------------------------------------------------------------------- /vue-playground/babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | presets: [ 3 | '@vue/cli-plugin-babel/preset' 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /vue-playground/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vue-playground", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "serve": "vue-cli-service serve", 7 | "build": "vue-cli-service build", 8 | "lint": "vue-cli-service lint" 9 | }, 10 | "dependencies": { 11 | "ant-design-vue": "^1.4.5", 12 | "axios": "^0.19.0", 13 | "core-js": "^3.3.2", 14 | "lodash.isequal": "^4.5.0", 15 | "vue": "^2.6.10" 16 | }, 17 | "devDependencies": { 18 | "@vue/cli-plugin-babel": "^4.0.0", 19 | "@vue/cli-plugin-eslint": "^4.0.0", 20 | "@vue/cli-service": "^4.0.0", 21 | "babel-eslint": "^10.0.3", 22 | "eslint": "^5.16.0", 23 | "eslint-plugin-vue": "^5.0.0", 24 | "vue-template-compiler": "^2.6.10" 25 | }, 26 | "eslintConfig": { 27 | "root": true, 28 | "env": { 29 | "node": true 30 | }, 31 | "extends": [ 32 | "plugin:vue/essential", 33 | "eslint:recommended" 34 | ], 35 | "rules": {}, 36 | "parserOptions": { 37 | "parser": "babel-eslint" 38 | } 39 | }, 40 | "postcss": { 41 | "plugins": { 42 | "autoprefixer": {} 43 | } 44 | }, 45 | "browserslist": [ 46 | "> 1%", 47 | "last 2 versions" 48 | ] 49 | } 50 | -------------------------------------------------------------------------------- /vue-playground/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiuquanwu/cloudbase-vue-next/fa892291a6155d583568ac5cbd0bc38fe336d7b8/vue-playground/public/favicon.ico -------------------------------------------------------------------------------- /vue-playground/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | vue-playground 9 | 10 | 11 | 14 |
15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /vue-playground/src/App.vue: -------------------------------------------------------------------------------- 1 | 28 | 29 | 68 | -------------------------------------------------------------------------------- /vue-playground/src/assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qiuquanwu/cloudbase-vue-next/fa892291a6155d583568ac5cbd0bc38fe336d7b8/vue-playground/src/assets/logo.png -------------------------------------------------------------------------------- /vue-playground/src/components/HelloWorld.vue: -------------------------------------------------------------------------------- 1 | 32 | 33 | 41 | 42 | 43 | 59 | -------------------------------------------------------------------------------- /vue-playground/src/main.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import App from './App.vue' 3 | 4 | Vue.config.productionTip = false 5 | 6 | new Vue({ 7 | render: h => h(App), 8 | }).$mount('#app') 9 | -------------------------------------------------------------------------------- /vue-playground/vue.config.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable no-undef */ 2 | module.exports = { 3 | chainWebpack: config => config.resolve.symlinks(false) 4 | } -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require("path") 2 | var webpack = require("webpack") 3 | var OptimizeCSSPlugin = require("optimize-css-assets-webpack-plugin") 4 | const VueLoaderPlugin = require("vue-loader/lib/plugin") 5 | 6 | module.exports = { 7 | entry: "./src/index.js", 8 | mode: "production", 9 | output: { 10 | path: path.resolve(__dirname, "./dist"), 11 | publicPath: "/dist/", 12 | filename: "index.js", 13 | library: "@cloudbase/vue-provider", 14 | libraryTarget: "umd", 15 | globalObject: "typeof self !== 'undefined' ? self : this" 16 | }, 17 | module: { 18 | rules: [ 19 | { 20 | test: /\.vue$/, 21 | loader: "vue-loader" 22 | }, 23 | { 24 | test: /\.js$/, 25 | loader: "babel-loader", 26 | exclude: /node_modules/ 27 | }, 28 | { 29 | test: /\.css$/, 30 | use: ["vue-style-loader", "css-loader"] 31 | } 32 | ] 33 | }, 34 | devtool: "#source-map", 35 | plugins: [new VueLoaderPlugin()] 36 | } 37 | --------------------------------------------------------------------------------