injext client
This commit is contained in:
parent
bc97d009ae
commit
0cbcab6597
5 changed files with 38 additions and 34 deletions
|
@ -1,20 +1,21 @@
|
||||||
import { paths } from './schema.ts'
|
import { paths } from './schema.ts'
|
||||||
import createClient, { Middleware } from 'openapi-fetch'
|
import createClient, { Client, Middleware } from 'openapi-fetch'
|
||||||
import { dispatchMessage } from '../messageBus/messageBus.ts'
|
import { dispatchMessage } from '../messageBus/messageBus.ts'
|
||||||
|
|
||||||
const client = createClient<paths>({ baseUrl: import.meta.env.VITE_API_URL })
|
export type ApiClient = Client<paths>
|
||||||
|
|
||||||
const UnauthorizedHandlerMiddleware: Middleware = {
|
export function initClient(): ApiClient {
|
||||||
async onResponse({ response }) {
|
const client = createClient<paths>({ baseUrl: import.meta.env.VITE_API_URL })
|
||||||
console.debug(response.headers.getSetCookie())
|
const UnauthorizedHandlerMiddleware: Middleware = {
|
||||||
console.debug(response.headers.get('set-cookie'))
|
async onResponse({ response }) {
|
||||||
if (response.status === 401) {
|
console.debug(response.headers.getSetCookie())
|
||||||
dispatchMessage('auth:unauthorized', null)
|
console.debug(response.headers.get('set-cookie'))
|
||||||
}
|
if (response.status === 401) {
|
||||||
},
|
dispatchMessage('auth:unauthorized', null)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
client.use(UnauthorizedHandlerMiddleware)
|
||||||
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
client.use(UnauthorizedHandlerMiddleware)
|
|
||||||
|
|
||||||
// todo inject this if necessary
|
|
||||||
export default client
|
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
import { dispatchMessage } from '../messageBus/messageBus.ts'
|
import { dispatchMessage } from '../messageBus/messageBus.ts'
|
||||||
import client from '../api/client.ts'
|
|
||||||
import { ProblemDetails } from '../../types'
|
import { ProblemDetails } from '../../types'
|
||||||
import { SignupCode } from './signupCode.ts'
|
import { SignupCode } from './signupCode.ts'
|
||||||
import { getCookie } from './cookies.ts'
|
import { getCookie } from './cookies.ts'
|
||||||
|
import { ApiClient } from '../api/client.ts'
|
||||||
|
|
||||||
export class AuthService {
|
export class AuthService {
|
||||||
constructor() {}
|
constructor(private readonly client: ApiClient) {}
|
||||||
|
|
||||||
async login(username: string, password: string) {
|
async login(username: string, password: string) {
|
||||||
const res = await client.POST('/auth/login', {
|
const res = await this.client.POST('/auth/login', {
|
||||||
body: { username, password },
|
body: { username, password },
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
})
|
})
|
||||||
|
@ -21,7 +21,7 @@ export class AuthService {
|
||||||
}
|
}
|
||||||
|
|
||||||
async signup(username: string, password: string, signupCode: string) {
|
async signup(username: string, password: string, signupCode: string) {
|
||||||
const res = await client.POST('/auth/register', {
|
const res = await this.client.POST('/auth/register', {
|
||||||
body: { username, password, signupCode, email: null },
|
body: { username, password, signupCode, email: null },
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
})
|
})
|
||||||
|
@ -35,13 +35,13 @@ export class AuthService {
|
||||||
}
|
}
|
||||||
|
|
||||||
async logout() {
|
async logout() {
|
||||||
await client.DELETE('/auth/session', { credentials: 'include' })
|
await this.client.DELETE('/auth/session', { credentials: 'include' })
|
||||||
|
|
||||||
dispatchMessage('auth:logged-out', null)
|
dispatchMessage('auth:logged-out', null)
|
||||||
}
|
}
|
||||||
|
|
||||||
async createSignupCode(code: string, email: string, name: string) {
|
async createSignupCode(code: string, email: string, name: string) {
|
||||||
const res = await client.POST('/auth/signup-codes', {
|
const res = await this.client.POST('/auth/signup-codes', {
|
||||||
body: { code, email, name },
|
body: { code, email, name },
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
})
|
})
|
||||||
|
@ -53,7 +53,7 @@ export class AuthService {
|
||||||
}
|
}
|
||||||
|
|
||||||
async listSignupCodes() {
|
async listSignupCodes() {
|
||||||
const res = await client.GET('/auth/signup-codes', {
|
const res = await this.client.GET('/auth/signup-codes', {
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ export class AuthService {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
await client.GET(`/auth/user/{userId}`, {
|
await this.client.GET(`/auth/user/{userId}`, {
|
||||||
params: {
|
params: {
|
||||||
path: { userId },
|
path: { userId },
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import { Post } from './posts.ts'
|
import { Post } from './posts.ts'
|
||||||
import client from '../../api/client.ts'
|
import { ApiClient } from '../../api/client.ts'
|
||||||
|
|
||||||
export class PostsService {
|
export class PostsService {
|
||||||
constructor() {}
|
constructor(private readonly client: ApiClient) {}
|
||||||
|
|
||||||
async createNew(
|
async createNew(
|
||||||
authorId: string,
|
authorId: string,
|
||||||
|
@ -10,7 +10,7 @@ export class PostsService {
|
||||||
media: CreatePostMedia[],
|
media: CreatePostMedia[],
|
||||||
isPublic: boolean,
|
isPublic: boolean,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const response = await client.POST('/posts', {
|
const response = await this.client.POST('/posts', {
|
||||||
body: {
|
body: {
|
||||||
authorId,
|
authorId,
|
||||||
content,
|
content,
|
||||||
|
@ -30,7 +30,7 @@ export class PostsService {
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadPublicFeed(cursor: string | null, amount: number | null): Promise<Post[]> {
|
async loadPublicFeed(cursor: string | null, amount: number | null): Promise<Post[]> {
|
||||||
const response = await client.GET('/posts', {
|
const response = await this.client.GET('/posts', {
|
||||||
query: { cursor, amount },
|
query: { cursor, amount },
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
})
|
})
|
||||||
|
@ -47,7 +47,7 @@ export class PostsService {
|
||||||
cursor: string | null,
|
cursor: string | null,
|
||||||
amount: number | null,
|
amount: number | null,
|
||||||
): Promise<Post[]> {
|
): Promise<Post[]> {
|
||||||
const response = await client.GET('/posts', {
|
const response = await this.client.GET('/posts', {
|
||||||
params: {
|
params: {
|
||||||
query: { From: cursor ?? undefined, Amount: amount ?? undefined, Author: username },
|
query: { From: cursor ?? undefined, Amount: amount ?? undefined, Author: username },
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import client from '../api/client.ts'
|
import { ApiClient } from '../api/client.ts'
|
||||||
|
|
||||||
export class MediaService {
|
export class MediaService {
|
||||||
constructor() {}
|
constructor(private readonly client: ApiClient) {}
|
||||||
async uploadFile(file: File): Promise<{ mediaId: string; url: URL }> {
|
async uploadFile(file: File): Promise<{ mediaId: string; url: URL }> {
|
||||||
const body = new FormData()
|
const body = new FormData()
|
||||||
body.append('file', file)
|
body.append('file', file)
|
||||||
|
|
||||||
const response = await client.POST('/media', {
|
const response = await this.client.POST('/media', {
|
||||||
// @ts-expect-error this endpoint takes multipart/form-data which means passing a FormData as the body
|
// @ts-expect-error this endpoint takes multipart/form-data which means passing a FormData as the body
|
||||||
// maybe openapi-fetch only wants to handle JSON? who knows
|
// maybe openapi-fetch only wants to handle JSON? who knows
|
||||||
body,
|
body,
|
||||||
|
|
|
@ -3,14 +3,17 @@ import { setGlobal } from './app/femtoApp.ts'
|
||||||
import { PostsService } from './app/feed/posts/postsService.ts'
|
import { PostsService } from './app/feed/posts/postsService.ts'
|
||||||
import { MediaService } from './app/media/mediaService.ts'
|
import { MediaService } from './app/media/mediaService.ts'
|
||||||
import { AuthService } from './app/auth/authService.ts'
|
import { AuthService } from './app/auth/authService.ts'
|
||||||
|
import { initClient } from './app/api/client.ts'
|
||||||
|
|
||||||
export function initApp() {
|
export function initApp() {
|
||||||
setGlobal('version', import.meta.env.VITE_FEMTO_VERSION)
|
setGlobal('version', import.meta.env.VITE_FEMTO_VERSION)
|
||||||
initUser()
|
initUser()
|
||||||
|
|
||||||
const postService = new PostsService()
|
const client = initClient()
|
||||||
const mediaService = new MediaService()
|
|
||||||
const authService = new AuthService()
|
const postService = new PostsService(client)
|
||||||
|
const mediaService = new MediaService(client)
|
||||||
|
const authService = new AuthService(client)
|
||||||
|
|
||||||
return { postService, mediaService, authService }
|
return { postService, mediaService, authService }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue