pook1574
pook1574
NNuxt
Created by pook1574 on 10/7/2024 in #❓・help
Why don't I have access to Pinia when I try to pull types?
No description
8 replies
NNuxt
Created by pook1574 on 10/3/2024 in #❓・help
Repository Pattern implementation attempt
import type { NitroFetchOptions } from "nitropack";
import type { IPaginationResponse } from "@/types/IPaginationResponse";
import type { DjangoPaginationQueryParams } from "@/types/DjangoPaginationQueryParams";
export abstract class BaseRepository<T> {
protected abstract baseUrl: string;
protected defaultFetch = $fetch; // Using Nuxt's global $fetch
async getAllPaginated(
queryParams: DjangoPaginationQueryParams,
fetcher = this.defaultFetch
): Promise<IPaginationResponse<T>> {
const options: NitroFetchOptions<"get"> = {
method: "get", // Using GET method without body
};
const queryString = new URLSearchParams(
Object.entries(queryParams).reduce((acc, [key, value]) => {
acc[key] = String(value);
return acc;
}, {} as Record<string, string>)
).toString();
const response = await fetcher<IPaginationResponse<T>>(
`${this.baseUrl}?${queryString}`, // Append query string to base URL
options
);
return response;
}

async create(payload: T, fetcher = this.defaultFetch): Promise<T> {
const data = await fetcher<T>(this.baseUrl, {
method: "POST",
body: JSON.stringify(payload),
headers: {
"Content-Type": "application/json",
},
});
return data;
}

async update(
id: number,
payload: T,
fetcher = this.defaultFetch
): Promise<T> {
const data = await fetcher<T>(`${this.baseUrl}/${id}`, {
method: "PUT",
body: JSON.stringify(payload),
headers: {
"Content-Type": "application/json",
},
});
return data;
}
async save(
entity: T & { id?: number },
fetcher = this.defaultFetch
): Promise<T> {
if (entity.id) {
return this.update(entity.id, entity, fetcher);
} else {
return this.create(entity, fetcher);
}
}
}
import type { NitroFetchOptions } from "nitropack";
import type { IPaginationResponse } from "@/types/IPaginationResponse";
import type { DjangoPaginationQueryParams } from "@/types/DjangoPaginationQueryParams";
export abstract class BaseRepository<T> {
protected abstract baseUrl: string;
protected defaultFetch = $fetch; // Using Nuxt's global $fetch
async getAllPaginated(
queryParams: DjangoPaginationQueryParams,
fetcher = this.defaultFetch
): Promise<IPaginationResponse<T>> {
const options: NitroFetchOptions<"get"> = {
method: "get", // Using GET method without body
};
const queryString = new URLSearchParams(
Object.entries(queryParams).reduce((acc, [key, value]) => {
acc[key] = String(value);
return acc;
}, {} as Record<string, string>)
).toString();
const response = await fetcher<IPaginationResponse<T>>(
`${this.baseUrl}?${queryString}`, // Append query string to base URL
options
);
return response;
}

async create(payload: T, fetcher = this.defaultFetch): Promise<T> {
const data = await fetcher<T>(this.baseUrl, {
method: "POST",
body: JSON.stringify(payload),
headers: {
"Content-Type": "application/json",
},
});
return data;
}

async update(
id: number,
payload: T,
fetcher = this.defaultFetch
): Promise<T> {
const data = await fetcher<T>(`${this.baseUrl}/${id}`, {
method: "PUT",
body: JSON.stringify(payload),
headers: {
"Content-Type": "application/json",
},
});
return data;
}
async save(
entity: T & { id?: number },
fetcher = this.defaultFetch
): Promise<T> {
if (entity.id) {
return this.update(entity.id, entity, fetcher);
} else {
return this.create(entity, fetcher);
}
}
}
7 replies
NNuxt
Created by pook1574 on 9/27/2024 in #❓・help
nuxt/storybook does not work
No description
3 replies
NNuxt
Created by pook1574 on 9/27/2024 in #❓・help
I have a new project and storybook module is not working.
No description
1 replies