Skip to content

[Improve code]: 型定義はjsonインポートではなく、モジュール内で行うべきである #88

@nekono-dev

Description

@nekono-dev

Where and how it seems bad?

atproto_apiに始まるfetch APIを実行するコード中で、*.jsonをインポートして型定義を行っているが、jsonインポートでの型定義は厳密ではない場合が多く、また名前も一定に定まらないため可読性にすぐれない

以下のようなコードは修正されるべきである

import getEndpoint, { com_atproto, app_bsky } from "./base"
import mtype from "./models/createRecord.json"
import etype from "./models/error.json"
const apiName = com_atproto.repo.createRecord
const endpoint = getEndpoint(apiName)

export const api = async ({
    repo,
    accessJwt,
    record,
}: {
    repo: string
    accessJwt: string
    record: object
}): Promise<typeof mtype | typeof etype> =>
...

What it shoud become?

jsonインポートに対応する型は、jsonインポートを利用するts内で型を定義するべきである。外部から型を読み込む場合も、モジュールから型をimportするべきである。

ただし #15 によりそもそもatproto_apiが不要になると思われる、優先度は低く設定している。
atproto_api以外の箇所については対応するべきである。

Metadata

Metadata

Assignees

No one assigned

    Labels

    RemindsLow Priority IssueinvalidThis doesn't seem right

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions