Skip to content

Commit

Permalink
Update api class types
Browse files Browse the repository at this point in the history
  • Loading branch information
zlwaterfield committed Dec 4, 2024
1 parent d0ca214 commit c5056ee
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 61 deletions.
25 changes: 21 additions & 4 deletions frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,10 @@ class ApiRequest {
return await api.update(this.assembleFullUrl(), options?.data, options)
}

public async put(options?: ApiMethodOptions & { data: any }): Promise<any> {
return await api.put(this.assembleFullUrl(), options?.data, options)
}

public async create(options?: ApiMethodOptions & { data: any }): Promise<any> {
return await api.create(this.assembleFullUrl(), options?.data, options)
}
Expand Down Expand Up @@ -2554,14 +2558,19 @@ const api = {
})
},

async update(url: string, data: any, options?: ApiMethodOptions): Promise<any> {
async _update<T = any, P = any>(
method: 'PATCH' | 'PUT',
url: string,
data: P,
options?: ApiMethodOptions
): Promise<T> {
url = prepareUrl(url)
ensureProjectIdNotInvalid(url)
const isFormData = data instanceof FormData

const response = await handleFetch(url, 'PATCH', async () => {
const response = await handleFetch(url, method, async () => {
return await fetch(url, {
method: 'PATCH',
method: method,
headers: {
...objectClean(options?.headers ?? {}),
...(isFormData ? {} : { 'Content-Type': 'application/json' }),
Expand All @@ -2576,7 +2585,15 @@ const api = {
return await getJSONOrNull(response)
},

async create(url: string, data?: any, options?: ApiMethodOptions): Promise<any> {
async update<T = any, P = any>(url: string, data: P, options?: ApiMethodOptions): Promise<T> {
return api._update('PATCH', url, data, options)
},

async put<T = any, P = any>(url: string, data: P, options?: ApiMethodOptions): Promise<T> {
return api._update('PUT', url, data, options)
},

async create<T = any, P = any>(url: string, data?: P, options?: ApiMethodOptions): Promise<T> {
const res = await api.createResponse(url, data, options)
return await getJSONOrNull(res)
},
Expand Down
29 changes: 16 additions & 13 deletions frontend/src/models/dashboardsModel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ export const dashboardsModel = kea<dashboardsModelType>([

const beforeChange = { ...values.rawDashboards[id] }

const response = (await api.update(
const response = await api.update<DashboardType>(
`api/environments/${teamLogic.values.currentTeamId}/dashboards/${id}`,
payload
)) as DashboardType
)
const updatedAttribute = Object.keys(payload)[0]
if (updatedAttribute === 'name' || updatedAttribute === 'description' || updatedAttribute === 'tags') {
eventUsageLogic.actions.reportDashboardFrontEndUpdate(
Expand All @@ -134,10 +134,10 @@ export const dashboardsModel = kea<dashboardsModelType>([
button: {
label: 'Undo',
action: async () => {
const reverted = (await api.update(
const reverted = await api.update<DashboardType>(
`api/environments/${teamLogic.values.currentTeamId}/dashboards/${id}`,
beforeChange
)) as DashboardType
)
actions.updateDashboardSuccess(getQueryBasedDashboard(reverted))
lemonToast.success('Dashboard change reverted')
},
Expand All @@ -160,31 +160,34 @@ export const dashboardsModel = kea<dashboardsModelType>([
})
) as DashboardType<QueryBasedInsightModel>,
pinDashboard: async ({ id, source }) => {
const response = (await api.update(
const response = await api.update(
`api/environments/${teamLogic.values.currentTeamId}/dashboards/${id}`,
{
pinned: true,
}
)) as DashboardType
)
eventUsageLogic.actions.reportDashboardPinToggled(true, source)
return getQueryBasedDashboard(response)!
},
unpinDashboard: async ({ id, source }) => {
const response = (await api.update(
const response = await api.update<DashboardType>(
`api/environments/${teamLogic.values.currentTeamId}/dashboards/${id}`,
{
pinned: false,
}
)) as DashboardType
)
eventUsageLogic.actions.reportDashboardPinToggled(false, source)
return getQueryBasedDashboard(response)!
},
duplicateDashboard: async ({ id, name, show, duplicateTiles }) => {
const result = (await api.create(`api/environments/${teamLogic.values.currentTeamId}/dashboards/`, {
use_dashboard: id,
name: `${name} (Copy)`,
duplicate_tiles: duplicateTiles,
})) as DashboardType
const result = await api.create<DashboardType>(
`api/environments/${teamLogic.values.currentTeamId}/dashboards/`,
{
use_dashboard: id,
name: `${name} (Copy)`,
duplicate_tiles: duplicateTiles,
}
)
if (show) {
router.actions.push(urls.dashboard(result.id))
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/scenes/authentication/login2FALogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const login2FALogic = kea<login2FALogicType>([
submit: async ({ token }, breakpoint) => {
breakpoint()
try {
return await api.create('api/login/token', { token })
return await api.create<any>('api/login/token', { token })
} catch (e) {
const { code, detail } = e as Record<string, any>
actions.setGeneralError(code, detail)
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/scenes/authentication/loginLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const loginLogic = kea<loginLogicType>([
}

breakpoint()
const response = await api.create('api/login/precheck', { email })
const response = await api.create<any>('api/login/precheck', { email })
return { status: 'completed', ...response }
},
},
Expand All @@ -102,7 +102,7 @@ export const loginLogic = kea<loginLogicType>([
submit: async ({ email, password }, breakpoint) => {
breakpoint()
try {
return await api.create('api/login', { email, password })
return await api.create<any>('api/login', { email, password })
} catch (e) {
const { code } = e as Record<string, any>
let { detail } = e as Record<string, any>
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/scenes/authentication/setup2FALogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const setup2FALogic = kea<setup2FALogicType>([
null as { backup_codes: string[] } | null,
{
generateBackupCodes: async () => {
return await api.create('api/users/@me/two_factor_backup_codes/')
return await api.create<any>('api/users/@me/two_factor_backup_codes/')
},
},
],
Expand All @@ -95,7 +95,7 @@ export const setup2FALogic = kea<setup2FALogicType>([
{
disable2FA: async () => {
try {
await api.create('api/users/@me/two_factor_disable/')
await api.create<any>('api/users/@me/two_factor_disable/')
return true
} catch (e) {
const { code, detail } = e as Record<string, any>
Expand All @@ -114,7 +114,7 @@ export const setup2FALogic = kea<setup2FALogicType>([
submit: async ({ token }, breakpoint) => {
breakpoint()
try {
return await api.create('api/users/@me/validate_2fa/', { token })
return await api.create<any>('api/users/@me/validate_2fa/', { token })
} catch (e) {
const { code, detail } = e as Record<string, any>
actions.setGeneralError(code, detail)
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/scenes/instance/SystemStatus/staffUsersLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export const staffUsersLogic = kea<staffUsersLogicType>([
actions.setStaffUsersToBeAdded([])
const newStaffUsers = await Promise.all(
staffUsersToBeAdded.map(
async (userUuid) =>
(await api.update(`api/users/${userUuid}`, { is_staff: true })) as UserType
async (userUuid) => await api.update<UserType>(`api/users/${userUuid}`, { is_staff: true })
)
)
const updatedAllUsers: UserType[] = [
Expand All @@ -45,7 +44,7 @@ export const staffUsersLogic = kea<staffUsersLogicType>([
return updatedAllUsers
},
deleteStaffUser: async ({ userUuid }) => {
await api.update(`api/users/${userUuid}`, { is_staff: false })
await api.update<UserType>(`api/users/${userUuid}`, { is_staff: false })
if (values.user?.uuid === userUuid) {
actions.loadUser() // Loads the main user object to properly reflect staff user changes
router.actions.push(urls.projectHomepage())
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/scenes/projectLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export const projectLogic = kea<projectLogicType>([
throw new Error('Current project has not been loaded yet, so it cannot be updated!')
}

const patchedProject = (await api.update(
const patchedProject = await api.update<ProjectType>(
`api/projects/${values.currentProject.id}`,
payload
)) as ProjectType
)
breakpoint()

// We need to reload current org (which lists its projects) in organizationLogic AND in userLogic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ export const verifiedDomainsLogic = kea<verifiedDomainsLogicType>([
(await api.get(`api/organizations/${values.currentOrganization?.id}/domains`))
.results as OrganizationDomainType[],
addVerifiedDomain: async (domain: string) => {
const response = await api.create(`api/organizations/${values.currentOrganization?.id}/domains`, {
domain,
})
const response = await api.create<OrganizationDomainType>(
`api/organizations/${values.currentOrganization?.id}/domains`,
{
domain,
}
)
return [response, ...values.verifiedDomains]
},
deleteVerifiedDomain: async (id: string) => {
Expand All @@ -93,18 +96,18 @@ export const verifiedDomainsLogic = kea<verifiedDomainsLogicType>([
false,
{
updateDomain: async (payload: OrganizationDomainUpdatePayload) => {
const response = await api.update(
const response = await api.update<OrganizationDomainType>(
`api/organizations/${values.currentOrganization?.id}/domains/${payload.id}`,
{ ...payload, id: undefined }
)
lemonToast.success('Domain updated successfully! Changes will take immediately.')
actions.replaceDomain(response as OrganizationDomainType)
actions.replaceDomain(response)
return false
},
verifyDomain: async () => {
const response = (await api.create(
const response = await api.create<OrganizationDomainType>(
`api/organizations/${values.currentOrganization?.id}/domains/${values.verifyModal}/verify`
)) as OrganizationDomainType
)
if (response.is_verified) {
lemonToast.success('Domain verified successfully.')
} else {
Expand Down Expand Up @@ -158,12 +161,12 @@ export const verifiedDomainsLogic = kea<verifiedDomainsLogicType>([
if (!id) {
return
}
const response = (await api.update(
const response = await api.update<OrganizationDomainType>(
`api/organizations/${values.currentOrganization?.id}/domains/${payload.id}`,
{
...updateParams,
}
)) as OrganizationDomainType
)
breakpoint()
actions.replaceDomain(response)
actions.setConfigureSAMLModalId(null)
Expand Down
15 changes: 11 additions & 4 deletions frontend/src/scenes/settings/organization/inviteLogic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { actions, connect, events, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { router, urlToAction } from 'kea-router'
import api from 'lib/api'
import api, { PaginatedResponse } from 'lib/api'
import { OrganizationMembershipLevel } from 'lib/constants'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { organizationLogic } from 'scenes/organizationLogic'
Expand Down Expand Up @@ -49,15 +49,18 @@ export const inviteLogic = kea<inviteLogicType>([
{
inviteTeamMembers: async () => {
if (!values.canSubmit) {
return { invites: [] }
return []
}

const payload: Pick<OrganizationInviteType, 'target_email' | 'first_name' | 'level' | 'message'>[] =
values.invitesToSend.filter((invite) => invite.target_email)
if (values.message) {
payload.forEach((payload) => (payload.message = values.message))
}
return await api.create('api/organizations/@current/invites/bulk/', payload)
return await api.create<OrganizationInviteType[]>(
'api/organizations/@current/invites/bulk/',
payload
)
},
},
],
Expand All @@ -66,7 +69,11 @@ export const inviteLogic = kea<inviteLogicType>([
{
loadInvites: async () => {
return organizationLogic.values.currentOrganization
? (await api.get('api/organizations/@current/invites/')).results
? (
await api.get<PaginatedResponse<OrganizationInviteType>>(
'api/organizations/@current/invites/'
)
).results
: []
},
deleteInvite: async (invite: OrganizationInviteType) => {
Expand Down
8 changes: 5 additions & 3 deletions frontend/src/scenes/userLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const userLogic = kea<userLogicType>([
{
loadUser: async () => {
try {
return await api.get('api/users/@me/')
return await api.get<UserType>('api/users/@me/')
} catch (error: any) {
console.error(error)
actions.loadUserFailure(error.message)
Expand All @@ -67,26 +67,28 @@ export const userLogic = kea<userLogicType>([
throw new Error('Current user has not been loaded yet, so it cannot be updated!')
}
try {
const response = await api.update('api/users/@me/', user)
const response = await api.update<UserType>('api/users/@me/', user)
successCallback && successCallback()
return response
} catch (error: any) {
console.error(error)
actions.updateUserFailure(error.message)
return values.user
}
},
setUserScenePersonalisation: async ({ scene, dashboard }) => {
if (!values.user) {
throw new Error('Current user has not been loaded yet, so it cannot be updated!')
}
try {
return await api.create('api/users/@me/scene_personalisation', {
return await api.create<UserType>('api/users/@me/scene_personalisation', {
scene,
dashboard,
})
} catch (error: any) {
console.error(error)
actions.updateUserFailure(error.message)
return values.user
}
},
},
Expand Down
Loading

0 comments on commit c5056ee

Please sign in to comment.