Skip to content
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

add registry ky types #23

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 17 additions & 27 deletions cli/auth/login/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Command } from "commander"
import { cliConfig } from "lib/cli-config"
import delay from "delay"
import { getKy } from "lib/registry-api/get-ky"
import type { EndpointResponse } from "lib/registry-api/endpoint-types"

export const registerAuthLogin = (program: Command) => {
program.commands
Expand All @@ -13,12 +12,9 @@ export const registerAuthLogin = (program: Command) => {
const ky = getKy()

const { login_page } = await ky
.post<EndpointResponse["sessions/login_page/create"]>(
"sessions/login_page/create",
{
json: {},
},
)
.post("sessions/login_page/create", {
json: {},
})
.json()

console.log("Please visit the following URL to log in:")
Expand All @@ -27,17 +23,14 @@ export const registerAuthLogin = (program: Command) => {
// Wait until we receive confirmation
while (true) {
const { login_page: new_login_page } = await ky
.post<EndpointResponse["sessions/login_page/get"]>(
"sessions/login_page/get",
{
json: {
login_page_id: login_page.login_page_id,
},
headers: {
Authorization: `Bearer ${login_page.login_page_auth_token}`,
},
.post("sessions/login_page/get", {
json: {
login_page_id: login_page.login_page_id,
},
)
headers: {
Authorization: `Bearer ${login_page.login_page_auth_token}`,
},
})
.json()

if (new_login_page.was_login_successful) {
Expand All @@ -53,17 +46,14 @@ export const registerAuthLogin = (program: Command) => {
}

const { session } = await ky
.post<EndpointResponse["sessions/login_page/exchange_for_cli_session"]>(
"sessions/login_page/exchange_for_cli_session",
{
json: {
login_page_id: login_page.login_page_id,
},
headers: {
Authorization: `Bearer ${login_page.login_page_auth_token}`,
},
.post("sessions/login_page/exchange_for_cli_session", {
json: {
login_page_id: login_page.login_page_id,
},
headers: {
Authorization: `Bearer ${login_page.login_page_auth_token}`,
},
)
})
.json()

cliConfig.set("sessionToken", session.token)
Expand Down
33 changes: 20 additions & 13 deletions cli/clone/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,7 @@ export const registerClone = (program: Command) => {
console.log(`Cloning ${author}/${snippetName}...`)

const packageFileList = await ky
.post<{
package_files: Array<{
package_file_id: string
package_release_id: string
file_path: string
created_at: string
}>
}>("package_files/list", {
.post("package_files/list", {
json: {
package_name: `${author}/${snippetName}`,
use_latest_version: true,
Expand All @@ -63,18 +56,26 @@ export const registerClone = (program: Command) => {
if (filePath.startsWith("dist/")) continue

const fileContent = await ky
.post<{
package_file: {
content_text: string
}
}>("package_files/get", {
.post("package_files/get", {
json: {
package_name: `${author}/${snippetName}`,
file_path: fileInfo.file_path,
},
})
.json()

if ("error" in fileContent) {
throw new Error(
`Failed to get file ${filePath}: ${fileContent.error.message}`,
)
}
// Check if we have the package file
if (!fileContent.ok || !fileContent.package_file) {
throw new Error(
`Failed to get file ${filePath}: No package file returned`,
)
}

const fullPath = path.join(dirPath, filePath)
const dirName = path.dirname(fullPath)

Expand All @@ -83,6 +84,12 @@ export const registerClone = (program: Command) => {
fs.mkdirSync(dirName, { recursive: true })
}

if (!fileContent.package_file.content_text) {
throw new Error(
`Failed to get file ${filePath}: No content text available`,
)
}

fs.writeFileSync(fullPath, fileContent.package_file.content_text)
}

Expand Down
33 changes: 23 additions & 10 deletions lib/registry-api/endpoint-types.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
export interface EndpointResponse {
export interface EndpointTypes {
"sessions/login_page/create": {
login_page: {
login_page_id: string
login_page_auth_token: string
url: string
requestJson: Record<string, never>
responseJson: {
login_page: {
login_page_id: string
login_page_auth_token: string
url: string
}
}
}
"sessions/login_page/get": {
login_page: {
was_login_successful: boolean
is_expired: boolean
requestJson: {
login_page_id: string
}
responseJson: {
login_page: {
was_login_successful: boolean
is_expired: boolean
}
}
}
"sessions/login_page/exchange_for_cli_session": {
session: {
token: string
requestJson: {
login_page_id: string
}
responseJson: {
session: {
token: string
}
}
}
}
Loading