-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
170 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface Blameable { | ||
createdBy?: string; | ||
updatedBy?: string; | ||
deletedBy?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface Item { | ||
"@context"?: string; | ||
"@type"?: string; | ||
"@id"?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface Timestampable { | ||
createdAt?: string; | ||
updatedAt?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |