Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite770bbc

Browse files
committed
feat: rename all services to actions and remove redundant
1 parentab898f4 commite770bbc

File tree

9 files changed

+452
-654
lines changed

9 files changed

+452
-654
lines changed

‎packages/action/README.md‎

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#DavstackService
1+
#DavstackAction
22

3-
DavstackService is simple and flexible library for building backend services with TypeScript.
3+
DavstackAction is simple and flexible library for building backend services with TypeScript.
44

5-
###Why Use DavstackService?
5+
###Why Use DavstackAction?
66

77
- 🏠 Simple and familiar syntax - middleware, input and outputs inspired by trpc procedures
88
- 🧩 Flexible - Works well with next js server actions as well as trpc
@@ -14,22 +14,47 @@ Davstack Service is simple and flexible library for building backend services wi
1414
npm install zod @davstack/service
1515
```
1616

17-
Visit the[DavStackService Docs](https://davstack.com/service/overview) for more information and examples, such as this[trpc usage example](https://davstack.com/service/trpc-usage-example).
17+
Visit the[DavStackAction Docs](https://davstack.com/service/overview) for more information and examples, such as this[trpc usage example](https://davstack.com/service/trpc-usage-example).
1818

1919
##Demo Usage
2020

2121
- The service definition replaces tRPC procedures, but the syntax is very similar.
2222
- Once the service is integrated into tRPC routers, the API is the same as any other tRPC router.
2323

24+
##directly calling service example
25+
26+
```tsx
27+
exportconst generatePdf=authedAction
28+
.input(z.object({ html:z.string() }))
29+
.query(async ({ctx,input })=> {
30+
// complex business logic here
31+
returnpdf;
32+
});
33+
34+
/**
35+
* safe call eg from front end (usign nextjs server actions)
36+
* - will run authed middleware
37+
* - will parse inputs/outputs if defined
38+
*/
39+
const pdf=awaitgeneratePdf({ html:'...' });
40+
41+
/**
42+
* raw call eg from backend such inside another action
43+
* - will NOT run middlweare
44+
* - will NOT parse inputs/outputs
45+
*/
46+
const pdf=awaitgeneratePdf.raw(ctx, { html:'...' });
47+
```
48+
2449
##Composing Services example
2550

2651
```ts
2752
// api/services/invoice.ts
28-
import {authedService,publicService }from'@/lib/service';
53+
import {authedAction,publicAction }from'@/lib/service';
2954

30-
//Service composed from range of other services:
55+
//Action composed from range of other services:
3156

32-
exportconst mailAiGeneratedInvoice=authedService
57+
exportconst mailAiGeneratedInvoice=authedAction
3358
.input(z.object({ to:z.string(), projectId:z.string() }))
3459
.query(async ({ctx,input })=> {
3560
awaitcheckSufficientCredits(ctx, { amount:10 });
@@ -48,21 +73,21 @@ export const mailAiGeneratedInvoice = authedService
4873
return'Invoice sent';
4974
});
5075

51-
exportconst generatePdf=authedService
76+
exportconst generatePdf=authedAction
5277
.input(z.object({ html:z.string() }))
5378
.query(async ({ctx,input })=> {
5479
// complex business logic here
5580
returnpdf;
5681
});
5782

58-
exportconst sendEmail=authedService
83+
exportconst sendEmail=authedAction
5984
.input(z.object({ to:z.string(), subject:z.string(), body:z.string() }))
6085
.query(async ({ctx,input })=> {
6186
// complex business logic here
6287
return'Email sent';
6388
});
6489

65-
exportconst checkSufficientCredits=authedService
90+
exportconst checkSufficientCredits=authedAction
6691
.input(z.object({ amount:z.number() }))
6792
.query(async ({ctx,input })=> {
6893
// complex business logic here
@@ -106,9 +131,9 @@ export type PublicServiceCtx = {
106131
exporttypeAuthedServiceCtx=Required<PublicServiceCtx>;
107132

108133
// export your services
109-
exportconstpublicService=service<PublicServiceCtx>();
134+
exportconstpublicAction=service<PublicServiceCtx>();
110135

111-
exportconstauthedService=service<AuthedServiceCtx>().use(
136+
exportconstauthedAction=service<AuthedServiceCtx>().use(
112137
async ({ctx,next })=> {
113138
// Only allows authenticate users to access this service
114139
if (!ctx.user) {
@@ -128,13 +153,13 @@ Import the public / authed service builders from the service
128153

129154
```ts
130155
// api/services/some-service.ts
131-
import {publicService,authedService }from'@/lib/service';
156+
import {publicAction,authedAction }from'@/lib/service';
132157

133-
exportconst getSomePublicData=publicService.query(async ({ctx })=> {
158+
exportconst getSomePublicData=publicAction.query(async ({ctx })=> {
134159
return'Public data';
135160
});
136161

137-
exportconst getSomeUserData=authedService.query(async ({ctx })=> {
162+
exportconst getSomeUserData=authedAction.query(async ({ctx })=> {
138163
// will throw an error if ctx.user is undefined
139164
return'Protected data';
140165
});
@@ -154,7 +179,7 @@ const getTasks = service()
154179
});
155180
```
156181

157-
###DirectService Usage
182+
###DirectAction Usage
158183

159184
Unlike tRPC procedures, services can be called directly from anywhere in your backend, including within other services.
160185

@@ -209,7 +234,7 @@ NOTE: it is recommended to use the `* as yourServicesName` syntax. Otherwise, ct
209234

210235
Davstack Store has been heavily inspired by[tRPC](https://trpc.io/), a fantastic library for building type-safe APIs. A big shout-out to the tRPC team for their amazing work.
211236

212-
Nick-Lucas, a tRPC contributor, inspired the creation of DavstackService with his[github comment](https://github.com/trpc/trpc/discussions/4839#discussioncomment-8224476). He suggested "making controllers minimal" and "to separate your business logic from the API logic", which is exactly what DavstackService aims to do.
237+
Nick-Lucas, a tRPC contributor, inspired the creation of DavstackAction with his[github comment](https://github.com/trpc/trpc/discussions/4839#discussioncomment-8224476). He suggested "making controllers minimal" and "to separate your business logic from the API logic", which is exactly what DavstackAction aims to do.
213238

214239
###Contributing
215240

‎packages/action/src/index.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export*from'./service';
2-
export*from'./trpc-helpers';
1+
export*from'./action';

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp