feat: create api service PAI
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import type { ApiService } from "@/types";
|
||||
import type { ApiService, ApiServiceCredentials } from "@/types";
|
||||
import { axios, handleApiError } from "..";
|
||||
|
||||
export interface FetchApiServicesResponse {
|
||||
@ -16,3 +16,31 @@ export const getApiServices = async (): Promise<FetchApiServicesResponse> => {
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export interface CreateApiServiceRequest {
|
||||
name: string;
|
||||
description: string;
|
||||
redirect_uris: string[];
|
||||
scopes: string[];
|
||||
grant_types: string[];
|
||||
is_active: boolean;
|
||||
}
|
||||
|
||||
export interface CreateApiServiceResponse {
|
||||
service: ApiService;
|
||||
credentials: ApiServiceCredentials;
|
||||
}
|
||||
|
||||
export const postApiService = async (
|
||||
req: CreateApiServiceRequest,
|
||||
): Promise<CreateApiServiceResponse> => {
|
||||
const response = await axios.post<CreateApiServiceResponse>(
|
||||
"/api/v1/admin/api-services",
|
||||
req,
|
||||
);
|
||||
|
||||
if (response.status !== 200 && response.status !== 201)
|
||||
throw await handleApiError(response);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
@ -1,18 +1,32 @@
|
||||
import { getApiServices } from "@/api/admin/apiServices";
|
||||
import type { ApiService } from "@/types";
|
||||
import {
|
||||
getApiServices,
|
||||
postApiService,
|
||||
type CreateApiServiceRequest,
|
||||
} from "@/api/admin/apiServices";
|
||||
import type { ApiService, ApiServiceCredentials } from "@/types";
|
||||
import { create } from "zustand";
|
||||
|
||||
interface IAdminState {
|
||||
apiServices: ApiService[];
|
||||
loadingApiServices: boolean;
|
||||
|
||||
createdCredentials: ApiServiceCredentials | null;
|
||||
creatingApiService: boolean;
|
||||
|
||||
fetchApiServices: () => Promise<void>;
|
||||
createApiService: (req: CreateApiServiceRequest) => Promise<void>;
|
||||
resetCredentials: () => void;
|
||||
}
|
||||
|
||||
export const useAdmin = create<IAdminState>((set) => ({
|
||||
apiServices: [],
|
||||
loadingApiServices: false,
|
||||
|
||||
createdCredentials: null,
|
||||
creatingApiService: false,
|
||||
|
||||
resetCredentials: () => set({ createdCredentials: null }),
|
||||
|
||||
fetchApiServices: async () => {
|
||||
set({ loadingApiServices: true });
|
||||
|
||||
@ -25,4 +39,17 @@ export const useAdmin = create<IAdminState>((set) => ({
|
||||
set({ loadingApiServices: false });
|
||||
}
|
||||
},
|
||||
|
||||
createApiService: async (req: CreateApiServiceRequest) => {
|
||||
set({ creatingApiService: true });
|
||||
|
||||
try {
|
||||
const response = await postApiService(req);
|
||||
set({ createdCredentials: response.credentials });
|
||||
} catch (err) {
|
||||
console.log("ERR: Failed to fetch services:", err);
|
||||
} finally {
|
||||
set({ creatingApiService: false });
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
@ -21,3 +21,8 @@ export interface ApiService {
|
||||
updated_at: string;
|
||||
is_active: boolean;
|
||||
}
|
||||
|
||||
export interface ApiServiceCredentials {
|
||||
client_id: string;
|
||||
client_secret: string;
|
||||
}
|
||||
|
Reference in New Issue
Block a user