Skip to content

Commit

Permalink
feat: create types and store
Browse files Browse the repository at this point in the history
  • Loading branch information
medusiora committed Mar 31, 2024
1 parent abc8572 commit 3480f8b
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 9 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Nuxt 3 Simple Starter - A simple starter for Nuxt 3

Nuxt 3 frontend template for third-party API integrations.

> "Everything can work!" - BigDaddy
Nuxt 3 frontend template for third-party API integrations based on JSON-LD.

## Features

Expand All @@ -13,7 +11,7 @@ Nuxt 3 frontend template for third-party API integrations.
- [ ] 🍍 [State & Store Management (Pinia)](https://pinia.vuejs.org/)
- [ ] 🍍 [Subscribing to the state](https://pinia.vuejs.org/core-concepts/state.html#Subscribing-to-the-state)
- [x] 🍀 [Vue Composition Collection (Vueuse)](https://vueuse.org/)
- [ ] 🆎 [Internationalization (i18n)](https://v8.i18n.nuxtjs.org/)
- [x] 🆎 [Internationalization (i18n)](https://v8.i18n.nuxtjs.org/)

## Setup

Expand Down
1 change: 1 addition & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default defineNuxtConfig({
"@nuxtjs/eslint-module",
"@vueuse/nuxt",
"@nuxtjs/i18n",
"@pinia/nuxt",
],

i18n: {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"postinstall": "nuxt prepare"
},
"dependencies": {
"@pinia/nuxt": "^0.5.1",
"nuxt": "^3.11.1",
"vue": "^3.4.21",
"vue-router": "^4.3.0"
Expand Down
44 changes: 39 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions stores/post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Post } from "~/types/post";

export const usePostStore = defineStore("post", () => {
const apiOperations = createApiOperation<Post>("/api/posts");

// Add your custom store properties here
// https://pinia.vuejs.org/core-concepts/#Setup-Stores

return {
...apiOperations,
};
});

if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(usePostStore, import.meta.hot));
}
5 changes: 5 additions & 0 deletions types/blameable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Blameable {
createdBy?: string;
updatedBy?: string;
deletedBy?: string;
}
11 changes: 11 additions & 0 deletions types/collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { View } from "~/types/view";

export interface PagedCollection<T> {
"@context"?: string;
"@type"?: string;
"@id"?: string;
"hydra:member": T[];
"hydra:search"?: object;
"hydra:totalItems"?: number;
"hydra:view": View;
}
17 changes: 17 additions & 0 deletions types/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
interface HydraError {
"@context"?: string;
"@type"?: string;
"hydra:title": string;
"hydra:description": string;
trace?: any;
}

interface ValidatorError {
title?: string;
detail?: string;
}

export interface ErrorResponse extends HydraError, ValidatorError {
code?: string;
message?: string;
}
5 changes: 5 additions & 0 deletions types/item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Item {
"@context"?: string;
"@type"?: string;
"@id"?: string;
}
9 changes: 9 additions & 0 deletions types/post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Item } from "~/types/item";
import type { Timestampable } from "~/types/timestampable";
import type { Blameable } from "~/types/blameable";

export interface Post extends Item, Timestampable, Blameable {
id?: number;
title?: string;
content?: string;
}
4 changes: 4 additions & 0 deletions types/timestampable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Timestampable {
createdAt?: string;
updatedAt?: string;
}
7 changes: 7 additions & 0 deletions types/view.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface View {
"@id": string;
"hydra:first": string;
"hydra:last": string;
"hydra:next": string;
"hydra:previous": string;
}
53 changes: 53 additions & 0 deletions utils/create-api-operation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { PagedCollection } from "~/types/collection";

export default function createApiOperation<T>(endpoint: string) {
function serialize(body: T | object): Record<string, any> {
return JSON.parse(JSON.stringify(body));
}

async function collection(params?: object) {
return await $fetch<PagedCollection<T>>(`${endpoint}`, {
params,
});
}

async function item(id: number) {
return await $fetch<T>(`${endpoint}/${id}`);
}

async function post(body: T | FormData) {
return await $fetch<T>(`${endpoint}`, {
method: "POST",
body: serialize(body),
});
}

async function patch(body: T extends { id: number } ? T : any) {
return await $fetch<T>(`${endpoint}/${body.id}`, {
method: "PATCH",
body: serialize(body),
});
}

async function put(body: T extends { id: number } ? T : any) {
return await $fetch<T>(`${endpoint}/${body.id}`, {
method: "PUT",
body: serialize(body),
});
}

async function remove(id: number) {
return await $fetch<T>(`${endpoint}/${id}`, {
method: "DELETE",
});
}

return {
collection,
item,
post,
patch,
put,
remove,
};
}

0 comments on commit 3480f8b

Please sign in to comment.