@burotan/strapi-client
Version:
Ultimate type supported api client for Strapi!
97 lines (80 loc) • 2.2 kB
text/typescript
/*
import QueryString from "qs";
import { StrapiQuery } from "../types/StrapiQuery";
import {
StrapiClientOptions,
StrapiResult,
StrapiURI,
} from "../types/StrapiOptions";
export class StrapiQueryBuilder<T> {
private name: string;
private options: StrapiClientOptions;
constructor(name: string, options: StrapiClientOptions) {
this.name = name;
this.options = options;
}
private url(options: StrapiURI) {
const schema = [this.options.url, "api", this.name];
if (options.id) schema.push(options.id);
let fullUrl = schema.join("/");
if (options.query) {
const qs = QueryString.stringify(options.query, {
encode: false,
});
fullUrl = `${fullUrl}?${qs}`;
}
return fullUrl;
}
public async fetch(
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH",
url: string,
data?: any
) {
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (this.options.token) {
headers.Authorization = `Bearer ${this.options.token}`;
}
const result = await fetch(url, {
method,
headers,
body: data && JSON.stringify({ data }),
});
return method !== "DELETE" ? result.json() : result.ok;
}
public async findMany(query?: StrapiQuery): Promise<StrapiResult<any[]>> {
const url = this.url({ query });
return await this.fetch("GET", url);
}
public async findOne(
id: any,
query?: StrapiQuery
): Promise<StrapiResult<any>> {
const url = this.url({ id, query });
return await this.fetch("GET", url);
}
public async create(
data: any,
query?: StrapiQuery
): Promise<StrapiResult<any>> {
const url = this.url({ query });
const result = await this.fetch("POST", url, data);
return result;
}
public async update(
id: any,
data: any,
query?: StrapiQuery
): Promise<StrapiResult<any>> {
const url = this.url({ id, query });
const result = await this.fetch("PUT", url, data);
return result;
}
public async delete(id: any): Promise<void> {
const url = this.url({ id });
const result = await this.fetch("DELETE", url);
return result;
}
}
*/