Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/finom/vovk

RESTful RPC for Next.js - Transforms Next.js into a powerful REST API platform with RPC capabilities.
https://github.com/finom/vovk

controller decorators nestjs nextjs nextjs13 nodejs rest-api service threading worker

Last synced: 29 days ago
JSON representation

RESTful RPC for Next.js - Transforms Next.js into a powerful REST API platform with RPC capabilities.

Awesome Lists containing this project

README

        





vovk


RESTful RPC for Next.js


Transforms Next.js into a powerful REST API platform with RPC capabilities.



ℹ️ An ultimate version of Vovk.ts is coming soon. Stay tuned!


Documentation    
Discord    
Code Examples    
vovk-zod    
vovk-hello-world    
vovk-react-native-example



npm version 
TypeScript 
Build status


Example back-end Controller Class:

```ts
// /src/modules/post/PostController.ts
import { get, prefix, type VovkRequest } from 'vovk';
import PostService from './PostService';

@prefix('posts')
export default class PostController {
/**
* Create a comment on a post
* POST /api/posts/:postId/comments
*/
@post(':postId/comments')
static async createComment(
// decorate NextRequest type with body and query types
req: VovkRequest<
{ content: string; userId: string },
{ notificationType: 'push' | 'email' }
>,
{ postId }: { postId: string } // params
) {
// use standard Next.js API to get body and query
const { content, userId } = await req.json();
const notificationType = req.nextUrl.searchParams.get('notificationType');

// perform the request to the database in a custom service
return PostService.createComment({
postId, content, userId, notificationType,
});
}
}
```

Example component that uses the auto-generated client library:

```tsx
'use client';
import { useState } from 'react';
import { PostController } from 'vovk-client';
import type { VovkReturnType } from 'vovk';

export default function Example() {
const [response, setResponse] = useState>();

return (
<>
setResponse(
await PostController.createComment({
body: {
content: 'Hello, World!',
userId: '1',
},
params: { postId: '69' },
query: { notificationType: 'push' }
})
)}
>
Post a comment

{JSON.stringify(response)}

>
);
}
```

Alternatively, the resource can be fetched wit the regular `fetch` function:

```ts
fetch('/api/posts/69?notificationType=push', {
method: 'POST',
body: JSON.stringify({
content: 'Hello, World!',
userId: '1',
}),
})
```