Skip to content

feat: generate typescript types from codersdk structs #1047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
revert changes to other files
  • Loading branch information
f0ssel committed Apr 18, 2022
commit 4e651422bb70ef929240ad4f6f99db0ef1d72dcc
8 changes: 4 additions & 4 deletions site/src/api/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios"
import { getApiKey, login, logout } from "."
import { GenerateAPIKeyResponse, LoginWithPasswordResponse } from "./types"
import { APIKeyResponse, LoginResponse } from "./types"

// Mock the axios module so that no real network requests are made, but rather
// we swap in a resolved or rejected value
Expand All @@ -10,9 +10,9 @@ jest.mock("axios")

describe("api.ts", () => {
describe("login", () => {
it("should return LoginWithPasswordResponse", async () => {
it("should return LoginResponse", async () => {
// given
const loginResponse: LoginWithPasswordResponse = {
const loginResponse: LoginResponse = {
session_token: "abc_123_test",
}
const axiosMockPost = jest.fn().mockImplementationOnce(() => {
Expand Down Expand Up @@ -87,7 +87,7 @@ describe("api.ts", () => {
describe("getApiKey", () => {
it("should return APIKeyResponse", async () => {
// given
const apiKeyResponse: GenerateAPIKeyResponse = {
const apiKeyResponse: APIKeyResponse = {
key: "abc_123_test",
}
const axiosMockPost = jest.fn().mockImplementationOnce(() => {
Expand Down
12 changes: 6 additions & 6 deletions site/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export namespace Workspace {
}
}

export const login = async (email: string, password: string): Promise<Types.LoginWithPasswordResponse> => {
export const login = async (email: string, password: string): Promise<Types.LoginResponse> => {
const payload = JSON.stringify({
email,
password,
})

const response = await axios.post<Types.LoginWithPasswordResponse>("/api/v2/users/login", payload, {
const response = await axios.post<Types.LoginResponse>("/api/v2/users/login", payload, {
headers: { ...CONTENT_TYPE_JSON },
})

Expand All @@ -60,8 +60,8 @@ export const logout = async (): Promise<void> => {
await axios.post("/api/v2/users/logout")
}

export const getUser = async (): Promise<Types.User> => {
const response = await axios.get<Types.User>("/api/v2/users/me")
export const getUser = async (): Promise<Types.UserResponse> => {
const response = await axios.get<Types.UserResponse>("/api/v2/users/me")
return response.data
}

Expand All @@ -71,7 +71,7 @@ export const getApiKey = async (): Promise<Types.APIKeyResponse> => {
}

export const getUsers = async (): Promise<Types.PagedUsers> => {
// const response = await axios.get<Types.User[]>("/api/v2/users")
// const response = await axios.get<Types.UserResponse[]>("/api/v2/users")
// return response.data
return Promise.resolve({
page: [MockUser, MockUser2],
Expand Down Expand Up @@ -104,7 +104,7 @@ export const putWorkspaceAutostop = async (
})
}

export const updateProfile = async (userId: string, data: Types.UpdateProfileRequest): Promise<Types.User> => {
export const updateProfile = async (userId: string, data: Types.UpdateProfileRequest): Promise<Types.UserResponse> => {
const response = await axios.put(`/api/v2/users/${userId}/profile`, data)
return response.data
}
6 changes: 3 additions & 3 deletions site/src/test_helpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Provisioner,
Template,
UserAgent,
User,
UserResponse,
Workspace,
WorkspaceAutostartRequest,
} from "../api/types"
Expand All @@ -19,15 +19,15 @@ export const MockBuildInfo: BuildInfoResponse = {
version: "v99.999.9999+c9cdf14",
}

export const MockUser: User = {
export const MockUser: UserResponse = {
name: "Test User",
id: "test-user",
username: "TestUser",
email: "test@coder.com",
created_at: "",
}

export const MockUser2: User = {
export const MockUser2: UserResponse = {
id: "test-user-2",
name: "Test User 2",
username: "TestUser2",
Expand Down
8 changes: 4 additions & 4 deletions site/src/xServices/auth/authXService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface AuthContext {
getUserError?: Error | unknown
authError?: Error | unknown
updateProfileError?: Error | unknown
me?: Types.User
me?: Types.UserResponse
}

export type AuthEvent =
Expand All @@ -29,13 +29,13 @@ export const authMachine =
events: {} as AuthEvent,
services: {} as {
getMe: {
data: Types.User
data: Types.UserResponse
}
signIn: {
data: Types.LoginWithPasswordResponse
data: Types.LoginResponse
}
updateProfile: {
data: Types.User
data: Types.UserResponse
}
},
},
Expand Down