Skip to content

Commit 096b129

Browse files
committed
docs(service): simplify examples
1 parent 52093ef commit 096b129

File tree

1 file changed

+9
-39
lines changed

1 file changed

+9
-39
lines changed

packages/service/README.md

+9-39
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
# Davstack Service
22

3-
Davstack Service is beautifly simple and flexible library for building backend services with TypeScript.
4-
5-
The API is heavily inspired by the [tRPC](https://trpc.io/) procedure builder, providing an extremely intuitive and familiar DX.
6-
7-
The key difference is that Davstack Service is a **service** builder, not a procedure API builder.
3+
Davstack Service is simple and flexible library for building backend services with TypeScript.
84

95
### Why Use Davstack Service?
106

11-
- **Full Reusability**: Services can be called directly from anywhere in your backend, including within other services, without the overhead of unnecessary API calls.
12-
13-
- **Flexible & Portable**: Services are lightweight wrappers around typescript functions, so they can be integrated into any backend eg Next.js server components / actions and support broad range of content types (eg files, streams).
14-
15-
- **Seamless Integration with tRPC**: Davstack Service is built to complement tRPC. You can easily turn your services into tRPC procedures / routers with 0 boilerplate.
7+
- 🏠 Simple and familiar syntax - middleware, input and outputs inspired by trpc procedures
8+
- 🧩 Flexible - Works well with next js server actions as well as trpc
9+
- ✅ Typescript first - inferred input/output types and middleware
1610

1711
### Installation
1812

@@ -27,6 +21,8 @@ Visit the [DavStack Service Docs](https://davstack.com/service/overview) for mor
2721
- The service definition replaces tRPC procedures, but the syntax is very similar.
2822
- Once the service is integrated into tRPC routers, the API is the same as any other tRPC router.
2923

24+
## Composing Services example
25+
3026
```ts
3127
// api/services/invoice.ts
3228
import { authedService, publicService } from '@/lib/service';
@@ -36,13 +32,10 @@ import { authedService, publicService } from '@/lib/service';
3632
export const mailAiGeneratedInvoice = authedService
3733
.input(z.object({ to: z.string(), projectId: z.string() }))
3834
.query(async ({ ctx, input }) => {
39-
// each service is called directly, no API calls
4035
await checkSufficientCredits(ctx, { amount: 10 });
4136

42-
// The inputs / outputs are type safe and validated by Zod
4337
const pdf = await generatePdf(ctx, { html: project.invoiceHtml });
4438

45-
// Services are just functions - so no limitaitons of content types (eg files, streams, etc)
4639
await sendEmail(ctx, {
4740
to: input.to,
4841
subject: 'Invoice',
@@ -55,9 +48,6 @@ export const mailAiGeneratedInvoice = authedService
5548
return 'Invoice sent';
5649
});
5750

58-
// Each service is a small, reusable function
59-
// Easy to test, easy to understand, easy to maintain
60-
6151
export const generatePdf = authedService
6252
.input(z.object({ html: z.string() }))
6353
.query(async ({ ctx, input }) => {
@@ -99,11 +89,9 @@ export const appRouter = createTRPCRouter({
9989
});
10090
```
10191

102-
## Getting Started
92+
### Middleware Example
10393

104-
### Set up services in your project
105-
106-
Define your services in a separate file, and export them for use in your backend.
94+
Define your services with reusable middleware in a separate file, and export them for reuse.
10795

10896
```ts
10997
// lib/service.ts
@@ -136,9 +124,7 @@ export function createServiceCtx() {
136124
}
137125
```
138126

139-
### Defining a Service
140-
141-
Import the public / authed service builders from the service file, and define your services. You can use the `query` or `mutation` methods to define the service function.
127+
Import the public / authed service builders from the service
142128

143129
```ts
144130
// api/services/some-service.ts
@@ -168,8 +154,6 @@ const getTasks = service()
168154
});
169155
```
170156

171-
### Using Services
172-
173157
### Direct Service Usage
174158

175159
Unlike tRPC procedures, services can be called directly from anywhere in your backend, including within other services.
@@ -221,20 +205,6 @@ const appRouter = t.router({
221205

222206
NOTE: it is recommended to use the `* as yourServicesName` syntax. Otherwise, ctrl+click on the tRPC client handler will navigate you to the app router file, instead of the specific service definition.
223207

224-
# Comparison with tRPC
225-
226-
## Similarities
227-
228-
The fluent API is heavily inspired by the [tRPC](https://trpc.io/) procedure builder, providing an extremely intuitive and familiar DX.
229-
230-
Services still have access to ctx from middleware, use input/output schemas, and outputs can also be inferred from the query/mutation function, just like tRPC.
231-
232-
## Differences
233-
234-
The key difference is that Davstack Service is a **service** builder, not a procedure API builder.
235-
236-
This brings several benefits by decoupling your _service logic_ (eg database read/write operations), from the _transport layer_ (eg REST or tRPC APIs).
237-
238208
### Acknowledgements
239209

240210
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.

0 commit comments

Comments
 (0)