@cloudwerk/service 0.0.1 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +122 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# @cloudwerk/service
|
|
2
|
+
|
|
3
|
+
Reusable services for Cloudwerk applications with local execution or extraction to separate Workers.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @cloudwerk/service
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
### 1. Define a service
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
// app/services/email/service.ts
|
|
17
|
+
import { defineService } from '@cloudwerk/service'
|
|
18
|
+
|
|
19
|
+
export default defineService({
|
|
20
|
+
methods: {
|
|
21
|
+
async send({ to, subject, body }) {
|
|
22
|
+
const response = await fetch('https://api.resend.com/emails', {
|
|
23
|
+
method: 'POST',
|
|
24
|
+
headers: {
|
|
25
|
+
'Authorization': `Bearer ${this.env.RESEND_API_KEY}`,
|
|
26
|
+
'Content-Type': 'application/json',
|
|
27
|
+
},
|
|
28
|
+
body: JSON.stringify({ to, subject, html: body }),
|
|
29
|
+
})
|
|
30
|
+
return response.json()
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
})
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### 2. Use the service
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { services } from '@cloudwerk/core/bindings'
|
|
40
|
+
|
|
41
|
+
const result = await services.email.send({
|
|
42
|
+
to: 'user@example.com',
|
|
43
|
+
subject: 'Hello',
|
|
44
|
+
body: '<h1>Welcome!</h1>',
|
|
45
|
+
})
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Service Modes
|
|
49
|
+
|
|
50
|
+
Configure how services run in `cloudwerk.config.ts`:
|
|
51
|
+
|
|
52
|
+
### Local Mode
|
|
53
|
+
|
|
54
|
+
Services run as direct function calls in the main Worker.
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
export default defineConfig({
|
|
58
|
+
services: { mode: 'local' },
|
|
59
|
+
})
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Extracted Mode
|
|
63
|
+
|
|
64
|
+
Services run as separate Workers using Cloudflare service bindings.
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
export default defineConfig({
|
|
68
|
+
services: { mode: 'extracted' },
|
|
69
|
+
})
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Hybrid Mode
|
|
73
|
+
|
|
74
|
+
Mix local and extracted services.
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
export default defineConfig({
|
|
78
|
+
services: {
|
|
79
|
+
mode: 'hybrid',
|
|
80
|
+
email: { mode: 'extracted' },
|
|
81
|
+
cache: { mode: 'local' },
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Lifecycle Hooks
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
export default defineService({
|
|
90
|
+
methods: {
|
|
91
|
+
async processPayment(orderId, amount) { /* ... */ },
|
|
92
|
+
},
|
|
93
|
+
hooks: {
|
|
94
|
+
onInit: async () => { /* Service initialized */ },
|
|
95
|
+
onBefore: async (method, args) => { /* Before each call */ },
|
|
96
|
+
onAfter: async (method, result) => { /* After success */ },
|
|
97
|
+
onError: async (method, error) => { /* On failure */ },
|
|
98
|
+
},
|
|
99
|
+
})
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Extraction Configuration
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
export default defineService({
|
|
106
|
+
methods: { /* ... */ },
|
|
107
|
+
config: {
|
|
108
|
+
extraction: {
|
|
109
|
+
workerName: 'email-service',
|
|
110
|
+
bindings: ['RESEND_API_KEY', 'DB'],
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
})
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Documentation
|
|
117
|
+
|
|
118
|
+
For complete documentation, visit the [Cloudwerk Services Guide](https://cloudwerk.dev/guides/services/).
|
|
119
|
+
|
|
120
|
+
## License
|
|
121
|
+
|
|
122
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudwerk/service",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Service extraction and RPC for Cloudwerk",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"dist"
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@cloudwerk/core": "0.
|
|
21
|
+
"@cloudwerk/core": "0.14.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"typescript": "^5.4.0",
|