Skip to content
Snippets Groups Projects
Commit 6fae700b authored by Chif Gergő's avatar Chif Gergő
Browse files

Add client example files

parent a2e8506e
No related branches found
No related tags found
2 merge requests!19fix type imports,!8Refactor structure
import Axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import { EmptyReq, LoginReq, RegisterReq, UserResponse } from './types';
type LoginQuery = (data?: LoginReq) => Promise<AxiosResponse<UserResponse>>;
type RegisterQuery = (data?: RegisterReq) => Promise<AxiosResponse<UserResponse>>;
type MeQuery = (data?: EmptyReq, authToken?: string) => Promise<AxiosResponse<UserResponse>>;
export type AuthClient = {
login: LoginQuery;
me: MeQuery;
register: RegisterQuery;
};
export function auth(config: AxiosRequestConfig): AuthClient {
const axios = Axios.create({
...config,
baseURL: `${config.baseURL}/api/auth`,
});
const login: LoginQuery = (data?: LoginReq): Promise<AxiosResponse<UserResponse>> => {
return axios.post<UserResponse>('/login', data);
};
const register: RegisterQuery = (data?: RegisterReq): Promise<AxiosResponse<UserResponse>> => {
return axios.post<UserResponse>('/register', data);
};
const me: MeQuery = (
data?: undefined,
authToken?: string,
): Promise<AxiosResponse<UserResponse>> => {
return axios.get<UserResponse>('/me', {
headers: { Authorization: `Bearer ${authToken}` },
});
};
return {
login,
register,
me,
};
}
import { AxiosRequestConfig } from 'axios';
import { auth, AuthClient } from './auth';
const DEFAULT_BASE_URL = '';
export function createClient(
baseURL = DEFAULT_BASE_URL,
configOverride?: AxiosRequestConfig,
): Client {
const config: AxiosRequestConfig = {
baseURL,
withCredentials: true,
...configOverride,
};
const client = {
auth: auth(config),
};
return client;
}
export type Client = {
auth: AuthClient;
};
import { Profile } from '../types/types';
export type EmptyReq = undefined;
export type LoginReq = {
username: string;
password: string;
};
export type RegisterReq = {
username: string;
password: string;
};
export type UserResponse = {
user: Profile;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment