Skip to main content

fetchPosts

Retrieves paginated posts sorted by views, likes, and creation date.
cursorPostId
string
Cursor for pagination. Pass the last post ID to fetch the next page of results.
Returns: Promise<{ rows: Post[], total: number }> Error Handling: Throws error if fetching fails. Error is logged and re-thrown.
import { fetchPosts } from '@/services/posts.service';

// Fetch first page
const data = await fetchPosts();
const posts = data.rows;

// Fetch next page
const lastPostId = posts[posts.length - 1].$id;
const nextPage = await fetchPosts(lastPostId);

fetchPostsByUserId

Retrieves all posts created by a specific user, sorted by creation date.
userId
string
required
The ID of the user whose posts to fetch
Returns: Promise<{ rows: Post[], total: number }> Error Handling: Throws error if fetching fails. Error is logged and re-thrown.
import { fetchPostsByUserId } from '@/services/posts.service';

const userPosts = await fetchPostsByUserId('user123');
const posts = userPosts.rows;

searchPosts

Searches posts by content, returning results sorted by views, likes, and creation date.
query
string
required
Search query string. If empty or whitespace only, returns all posts via fetchPosts()
Returns: Promise<{ rows: Post[], total: number }> Error Handling: Throws error if search fails. Error is logged and re-thrown.
import { searchPosts } from '@/services/posts.service';

const results = await searchPosts('cricket world cup');
const posts = results.rows;

createPost

Creates a new cricket discussion post.
content
string
required
The post content
image
string[]
Array of image URLs. Defaults to empty array.
authorId
string
required
ID of the post author
authorName
string
required
Name of the post author
Returns: Promise<Post> Error Handling: Throws error if creation fails. Error is logged and re-thrown.
import { createPost } from '@/services/posts.service';

const post = await createPost({
  content: 'What an amazing match!',
  image: ['https://example.com/image.jpg'],
  authorId: 'user123',
  authorName: 'John Doe'
});

updatePost

Updates an existing post with partial data.
id
string
required
ID of the post to update
postData
Partial<Post>
required
Partial post data to update
Returns: Promise<Post> Error Handling: Throws error if update fails. Error is logged and re-thrown.
import { updatePost } from '@/services/posts.service';

const updated = await updatePost('post123', {
  content: 'Updated content',
  likes: 10
});

deletePost

Deletes a post by ID.
postId
string
required
ID of the post to delete
Returns: Promise<void> Error Handling: Throws error if deletion fails. Error is logged and re-thrown.
import { deletePost } from '@/services/posts.service';

await deletePost('post123');

executePost

Executes a post action through the posts guard function. This is the recommended way to perform post operations as it includes proper validation and authorization.
action
'create' | 'update' | 'delete' | 'like' | 'view'
required
The action to perform on the post
content
string
Post content (required for create and update actions)
postId
string
Post ID (required for update, delete, like, and view actions)
images
string[]
Array of image URLs (optional for create and update actions)
Returns: Promise<Execution> Error Handling: Throws error if execution fails or status is “failed”. Error is logged and re-thrown.
import { executePost } from '@/services/posts.service';

// Create a post
await executePost({
  action: 'create',
  content: 'What an amazing match!',
  images: ['https://example.com/image.jpg']
});

// Like a post
await executePost({
  action: 'like',
  postId: 'post123'
});

// View a post
await executePost({
  action: 'view',
  postId: 'post123'
});

// Delete a post
await executePost({
  action: 'delete',
  postId: 'post123'
});

Build docs developers (and LLMs) love