Add endpoint for resolving character conflicts
This commit is contained in:
parent
4d8a7d4007
commit
3e0529a46d
1 changed files with 109 additions and 92 deletions
191
utils/api.tsx
191
utils/api.tsx
|
|
@ -1,9 +1,10 @@
|
||||||
import axios, { Axios, AxiosRequestConfig, AxiosResponse } from "axios"
|
import axios, { Axios, AxiosRequestConfig, AxiosResponse } from "axios"
|
||||||
|
|
||||||
interface Entity {
|
interface Entity {
|
||||||
name: string
|
name: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prettier-ignore
|
||||||
type CollectionEndpoint = (params?: {}) => Promise<AxiosResponse<any>>
|
type CollectionEndpoint = (params?: {}) => Promise<AxiosResponse<any>>
|
||||||
type IdEndpoint = ({ id, params }: { id: string, params?: {} }) => Promise<AxiosResponse<any>>
|
type IdEndpoint = ({ id, params }: { id: string, params?: {} }) => Promise<AxiosResponse<any>>
|
||||||
type IdWithObjectEndpoint = ({ id, object, params }: { id: string, object: string, params?: {} }) => Promise<AxiosResponse<any>>
|
type IdWithObjectEndpoint = ({ id, object, params }: { id: string, object: string, params?: {} }) => Promise<AxiosResponse<any>>
|
||||||
|
|
@ -12,101 +13,117 @@ type PutEndpoint = (id: string, object: {}, headers?: {}) => Promise<AxiosRespo
|
||||||
type DestroyEndpoint = ({ id, params }: { id: string, params?: {} }) => Promise<AxiosResponse<any>>
|
type DestroyEndpoint = ({ id, params }: { id: string, params?: {} }) => Promise<AxiosResponse<any>>
|
||||||
|
|
||||||
interface EndpointMap {
|
interface EndpointMap {
|
||||||
getAll: CollectionEndpoint
|
getAll: CollectionEndpoint
|
||||||
getOne: IdEndpoint
|
getOne: IdEndpoint
|
||||||
getOneWithObject: IdWithObjectEndpoint
|
getOneWithObject: IdWithObjectEndpoint
|
||||||
create: PostEndpoint
|
create: PostEndpoint
|
||||||
update: PutEndpoint
|
update: PutEndpoint
|
||||||
destroy: DestroyEndpoint
|
destroy: DestroyEndpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
class Api {
|
class Api {
|
||||||
url: string
|
url: string
|
||||||
endpoints: { [key: string]: EndpointMap }
|
endpoints: { [key: string]: EndpointMap }
|
||||||
|
|
||||||
constructor({url}: {url: string}) {
|
constructor({url}: {url: string}) {
|
||||||
this.url = url
|
this.url = url
|
||||||
this.endpoints = {}
|
this.endpoints = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
createEntity(entity: Entity) {
|
||||||
|
this.endpoints[entity.name] = this.createEndpoints(entity)
|
||||||
|
}
|
||||||
|
|
||||||
|
createEntities(entities: Entity[]) {
|
||||||
|
entities.forEach(this.createEntity.bind(this))
|
||||||
|
}
|
||||||
|
|
||||||
|
createEndpoints({name}: {name: string}) {
|
||||||
|
const resourceUrl = `${this.url}/${name}`
|
||||||
|
|
||||||
|
return {
|
||||||
|
getAll: (params?: {}) => axios.get(resourceUrl, params),
|
||||||
|
getOne: ({ id, params }: { id: string, params?: {} }) => axios.get(`${resourceUrl}/${id}/`, params),
|
||||||
|
getOneWithObject: ({ id, object, params }: { id: string, object: string, params?: {} }) => axios.get(`${resourceUrl}/${id}/${object}`, params),
|
||||||
|
create: (object: {}, headers?: {}) => axios.post(resourceUrl, object, headers),
|
||||||
|
update: (id: string, object: {}, headers?: {}) => axios.put(`${resourceUrl}/${id}`, object, headers),
|
||||||
|
destroy: ({ id, params }: { id: string, params?: {} }) => axios.delete(`${resourceUrl}/${id}`, params)
|
||||||
|
} as EndpointMap
|
||||||
|
}
|
||||||
|
|
||||||
|
login(object: {}) {
|
||||||
|
const oauthUrl = process.env.NEXT_PUBLIC_SIERO_OAUTH_URL || 'https://localhost:3000/oauth'
|
||||||
|
return axios.post(`${ oauthUrl }/token`, object)
|
||||||
|
}
|
||||||
|
|
||||||
|
search({ object, query, filters, locale = "en", page = 0 }:
|
||||||
|
{ object: string, query: string, filters?: { [key: string]: number[] }, locale?: string, page?: number }) {
|
||||||
|
const resourceUrl = `${this.url}/${name}`
|
||||||
|
return axios.post(`${resourceUrl}search/${object}`, {
|
||||||
|
search: {
|
||||||
|
query: query,
|
||||||
|
filters: filters,
|
||||||
|
locale: locale,
|
||||||
|
page: page
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
check(resource: string, value: string) {
|
||||||
|
const resourceUrl = `${this.url}/check/${resource}`
|
||||||
|
return axios.post(resourceUrl, {
|
||||||
|
[resource]: value
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
resolveCharacterConflict({ incoming, conflicting, position, params }: {
|
||||||
|
incoming: string
|
||||||
|
conflicting: string[]
|
||||||
|
position: number,
|
||||||
|
params?: {}
|
||||||
|
}) {
|
||||||
|
const body = {
|
||||||
|
resolve: {
|
||||||
|
incoming: incoming,
|
||||||
|
conflicting: conflicting,
|
||||||
|
position: position,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
const resourceUrl = `${this.url}/characters/resolve`
|
||||||
|
return axios.post(resourceUrl, body, { headers: params })
|
||||||
|
}
|
||||||
|
savedTeams(params: {}) {
|
||||||
|
const resourceUrl = `${this.url}/parties/favorites`
|
||||||
|
return axios.get(resourceUrl, params)
|
||||||
|
}
|
||||||
|
|
||||||
createEntity(entity: Entity) {
|
saveTeam({ id, params }: { id: string, params?: {} }) {
|
||||||
this.endpoints[entity.name] = this.createEndpoints(entity)
|
const body = { favorite: { party_id: id } }
|
||||||
}
|
const resourceUrl = `${this.url}/favorites`
|
||||||
|
return axios.post(resourceUrl, body, { headers: params })
|
||||||
|
}
|
||||||
|
|
||||||
createEntities(entities: Entity[]) {
|
unsaveTeam({ id, params }: { id: string, params?: {} }) {
|
||||||
entities.forEach(this.createEntity.bind(this))
|
const body = { favorite: { party_id: id } }
|
||||||
}
|
const resourceUrl = `${this.url}/favorites`
|
||||||
|
return axios.delete(resourceUrl, { data: body, headers: params })
|
||||||
|
}
|
||||||
|
|
||||||
createEndpoints({name}: {name: string}) {
|
updateUncap(resource: 'character'|'weapon'|'summon', id: string, value: number) {
|
||||||
const resourceUrl = `${this.url}/${name}`
|
const pluralized = resource + 's'
|
||||||
|
const resourceUrl = `${this.url}/${pluralized}/update_uncap`
|
||||||
|
return axios.post(resourceUrl, {
|
||||||
|
[resource]: {
|
||||||
|
id: id,
|
||||||
|
uncap_level: value
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
userInfo(id: string) {
|
||||||
getAll: (params?: {}) => axios.get(resourceUrl, params),
|
const resourceUrl = `${this.url}/users/info/${id}`
|
||||||
getOne: ({ id, params }: { id: string, params?: {} }) => axios.get(`${resourceUrl}/${id}/`, params),
|
return axios.get(resourceUrl)
|
||||||
getOneWithObject: ({ id, object, params }: { id: string, object: string, params?: {} }) => axios.get(`${resourceUrl}/${id}/${object}`, params),
|
}
|
||||||
create: (object: {}, headers?: {}) => axios.post(resourceUrl, object, headers),
|
|
||||||
update: (id: string, object: {}, headers?: {}) => axios.put(`${resourceUrl}/${id}`, object, headers),
|
|
||||||
destroy: ({ id, params }: { id: string, params?: {} }) => axios.delete(`${resourceUrl}/${id}`, params)
|
|
||||||
} as EndpointMap
|
|
||||||
}
|
|
||||||
|
|
||||||
login(object: {}) {
|
|
||||||
const oauthUrl = process.env.NEXT_PUBLIC_SIERO_OAUTH_URL || 'https://localhost:3000/oauth'
|
|
||||||
return axios.post(`${ oauthUrl }/token`, object)
|
|
||||||
}
|
|
||||||
|
|
||||||
search({ object, query, filters, locale = "en", page = 0 }:
|
|
||||||
{ object: string, query: string, filters?: { [key: string]: number[] }, locale?: string, page?: number }) {
|
|
||||||
const resourceUrl = `${this.url}/${name}`
|
|
||||||
return axios.post(`${resourceUrl}search/${object}`, {
|
|
||||||
search: {
|
|
||||||
query: query,
|
|
||||||
filters: filters,
|
|
||||||
locale: locale,
|
|
||||||
page: page
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
check(resource: string, value: string) {
|
|
||||||
const resourceUrl = `${this.url}/check/${resource}`
|
|
||||||
return axios.post(resourceUrl, {
|
|
||||||
[resource]: value
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
savedTeams(params: {}) {
|
|
||||||
const resourceUrl = `${this.url}/parties/favorites`
|
|
||||||
return axios.get(resourceUrl, params)
|
|
||||||
}
|
|
||||||
|
|
||||||
saveTeam({ id, params }: { id: string, params?: {} }) {
|
|
||||||
const body = { favorite: { party_id: id } }
|
|
||||||
const resourceUrl = `${this.url}/favorites`
|
|
||||||
return axios.post(resourceUrl, body, { headers: params })
|
|
||||||
}
|
|
||||||
|
|
||||||
unsaveTeam({ id, params }: { id: string, params?: {} }) {
|
|
||||||
const body = { favorite: { party_id: id } }
|
|
||||||
const resourceUrl = `${this.url}/favorites`
|
|
||||||
return axios.delete(resourceUrl, { data: body, headers: params })
|
|
||||||
}
|
|
||||||
|
|
||||||
updateUncap(resource: 'character'|'weapon'|'summon', id: string, value: number) {
|
|
||||||
const pluralized = resource + 's'
|
|
||||||
const resourceUrl = `${this.url}/${pluralized}/update_uncap`
|
|
||||||
return axios.post(resourceUrl, {
|
|
||||||
[resource]: {
|
|
||||||
id: id,
|
|
||||||
uncap_level: value
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
userInfo(id: string) {
|
|
||||||
const resourceUrl = `${this.url}/users/info/${id}`
|
|
||||||
return axios.get(resourceUrl)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const api: Api = new Api({ url: process.env.NEXT_PUBLIC_SIERO_API_URL || 'https://localhost:3000/api/v1'})
|
const api: Api = new Api({ url: process.env.NEXT_PUBLIC_SIERO_API_URL || 'https://localhost:3000/api/v1'})
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue