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

[Improve code]: lib/* 手動formatter・Linter対応 #84

Merged
merged 29 commits into from
Apr 12, 2024
Merged

Conversation

ZEKE320
Copy link
Collaborator

@ZEKE320 ZEKE320 commented Apr 7, 2024

Resolves #58

本PRで行われた変更

  • 再代入のない変数にconst宣言を追加 (6c056c3)
  • Astroの環境変数を文字列とみなす記述を追加 (a7a4af9)
  • Nullish判定を明記 (08cb5fe)
  • コメント内の空白形式を変更 (a33a328)
  • 変数の型情報を明記 (e8bc360)
  • Prettierによるコードの整形 (*.tsx, *.astroの変更なし)
  • ファイル名getIdの綴りを修正 (e5835ec)
  • Astro API用の型をZodで再定義 (c0b67f9)
  • Astro APIのレスポンス取得箇所にZodのバリデーションチェックを実装 (700b531, 3e1565a)
  • pagedbAPIのレスポンスに用いられる型名が、モジュール間で重複しないよう変更 (bad010e)
  • APIレスポンスの型定義厳密化に伴い、影響箇所を修正 (3e1565a)
  • getOgpMetaエラー時のレスポンス処理で不足していたstatusを追加 (dcaed42)

@ZEKE320 ZEKE320 self-assigned this Apr 7, 2024
@ZEKE320 ZEKE320 linked an issue Apr 7, 2024 that may be closed by this pull request
9 tasks
@ZEKE320 ZEKE320 requested a review from nkte8 April 7, 2024 14:15
@ZEKE320
Copy link
Collaborator Author

ZEKE320 commented Apr 7, 2024

ひとまずESLintのエラーを解消し終わりました
レビューをお願いします!

Copy link
Owner

@nkte8 nkte8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

対応ありがとうございます!確認・必要に応じて対応お願いします。

エラーの出し方(エラーハンドリングの際に返却するe.namee.descriptionの値)については明確にどうするかをまだ決めていなかったので、いったんはこのままにしておきます。

Comment on lines 6 to 8
const PageCreationOutputZod = z.object({
uri: z.string(),
})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zodの対応ありがとうございます! #79 ではZodは冠詞的にくっつけていたのが、こちらでは接尾についているようです。こちらは #79 とは違った意図がある感じでしょうか?
(理由がないのであればどちらかに統一した方がいいのかなと @nkte8 は思っている感じです。)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

気づいていませんでした... Zod接頭辞 + [型名]で統一しておきますね

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全体のZodオブジェクトについて、#79 ではZodは冠詞的にくっつけていたのが、こちらでは接尾についているようです。こちらは #79 とは違った意図がある感じでしょうか?
(理由がないのであればどちらかに統一した方がいいのかなと @nkte8 は思っている感じです。)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 116 to 121
const errorObject: errorResponse & { html: string } = {
type: "error",
error: name,
message: msg,
status: 500,
html: responseHTML,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一時期テストに使っていたコードが残ってしまっていたみたいです...
html: stringの戻り値を消していただきたいです。作業範囲的に対象外なので恐縮ですが、よろしくお願いいたします。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

対応しました!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#79 ではZodは冠詞的にくっつけていたのが、こちらでは接尾についているようです。こちらは #79 とは違った意図がある感じでしょうか?
(理由がないのであればどちらかに統一した方がいいのかなと @nkte8 は思っている感じです。)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

リネーム対応ありがとうございます!気づきませんでした...

#79 ではZodは冠詞的にくっつけていたのが、こちらでは接尾についているようです。こちらは #79 とは違った意図がある感じでしょうか?
(理由がないのであればどちらかに統一した方がいいのかなと @nkte8 は思っている感じです。)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#79 ではZodは冠詞的にくっつけていたのが、こちらでは接尾についているようです。こちらは #79 とは違った意図がある感じでしょうか?
(理由がないのであればどちらかに統一した方がいいのかなと @nkte8 は思っている感じです。)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getPage.tsの方に埋め込む形になったと思うので、消してしまいましょうか。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

対応しました!


if (typeof getpage_res?.error !== "undefined" && getpage_res.error !== "") {
if ("error" in getPageResult && typeof getPageResult?.error !== "undefined" && getPageResult.error !== "") {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

作業範囲的に対象外の修正なので恐縮です。getPageAPIはskyshare定義のAPIなので、戻り型の検証は型特定部分だけで良いと思いました。getPage.tsを見る限り、エラーの場合は必ずerrormessageを持つレスポンスを行うためです。
なので条件式は("error" in getPageResult)でもいいかもと思っています、ご意見伺いたいです。

(typeof getpage_res?.error !== "undefined" && getpage_res.error !== "")という回りくどい書き方をしていたのは、原本を作成した @nkte8in演算子を知らなかったためです...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

了解です!
対応しました

Copy link

cloudflare-pages bot commented Apr 10, 2024

Deploying skyshare with  Cloudflare Pages  Cloudflare Pages

Latest commit: 62e71ac
Status: ✅  Deploy successful!
Preview URL: https://f77f46cd.skyshare.pages.dev
Branch Preview URL: https://preview-issue-58.skyshare.pages.dev

View logs

@ZEKE320
Copy link
Collaborator Author

ZEKE320 commented Apr 10, 2024

  • 967b327 Zodオブジェクト命名規則の統一
  • 4fd4e58 getOgpMetaのエラーレスポンスからhtmlを削除
  • ce937a7 使用されていないレスポンス型定義用のJSONを削除
  • 0fc4c2d pages/post/[slug].astroエラー型判定の実装を改善

上記4点対応済みです。確認をお願いします!

@ZEKE320 ZEKE320 requested a review from nkte8 April 10, 2024 13:12
@ZEKE320
Copy link
Collaborator Author

ZEKE320 commented Apr 10, 2024

@nkte8
今のタイミングで、エラーの型判定箇所を統一しておきたいですね

こちらで一度、方針を伝えていただけるとありがたいです!

@nkte8
Copy link
Owner

nkte8 commented Apr 10, 2024

@ZEKE320

エラーの型判定箇所を統一しておきたいですね

上について、#83 (comment) での議論通り、error in valueでの判定に揃えていただいてOKです。以下コメントです

  • getOgpMeta関数については、ATProtoでのエラー時のレスポンスを同じような形式、error,messageで返すようにしているので、これと同じ方針を取っていただいて大丈夫です!
    • 現在はtypeによりユニオン型として処理可能なようにしていますが、この方法でパラメータを外せそうですね。これは別途Issueを上げようと思っていますが、もし可能そうであれば今対処してしまってもいいと思います。
  • getOgpBlobについても、現在はレスポンス自体発生しない作りになってしまっていますが、error in valueの判定によって、クライアント側でエラーハンドリングができそうな気がしますね
    • 上と同じ題目でIssue上げようと思います。getOgpMeta同様に、もし可能そうであれば今対処してしまってもいいと思います。
    • いずれも作業範囲が広がりすぎるような感じでしたら別Issue対応でいいかなと思っています。

@ZEKE320
Copy link
Collaborator Author

ZEKE320 commented Apr 11, 2024

@nkte8

getOgpMeta関数については、ATProtoでのエラー時のレスポンスを同じような形式、error,messageで返すようにしているので、これと同じ方針を取っていただいて大丈夫です!

こちらの件ですが、あまり良い実装例が思い浮かばず...すみません。
一度別のIssueでお願いしたいと思います。


@nkte8
また上記とは別で、実装方針を伺いたい点があり

// PageDeleteButton.tsx(32行目)

if (!("error" in resDeletePage) &&
    resDeletePage.result === "ok") {
  /* ... */
} else {
  let e: Error = new Error((resDeletePage as typeof etype).message)
  /* ... */
}

ここで条件式をif (!("error" in resDeletePage))とした場合、resDeletePage as typeof etypeの型アノテーションを削除できるのですが、おそらくresDeletePage.result === "ok"も判定したいということですよね

  1. "error"resDeletePageのキーに含まれる場合
    → エラー(原因A)をスロー
  2. "error"resDeletePageのキーには含まれないが、resDeletePage.result"ok"ではない場合
    → エラー(原因B)をスロー
  3. "error"resDeletePageのキーに含まれず、resDeletePage.result"ok"の場合
    → 処理を続行

の3つで場合分けをする方針で進めましょうか?

@nkte8
Copy link
Owner

nkte8 commented Apr 11, 2024

@zeke 確認ありがとうございます!

こちらの件ですが、あまり良い実装例が思い浮かばず...すみません。
一度別のIssueでお願いしたいと思います。

承知です!

ここで条件式をif (!("error" in resDeletePage))とした場合、resDeletePage as typeof etypeの型アノテーションを削除できるのですが、おそらくresDeletePage.result === "ok"も判定したいということですよね

ここなんですけど、そもそも元の処理が以下のようになっています。
firebase/functions/src/editDbEndpoint/entrypoint.prod.ts

...
        if (resPagedbdel === null) {
            response.status(500).send(errRes(500))
            return
        }
        if (typeof resPagedbdel.result === "undefined") {
            response.status(502).send(errRes(502))
            return
        }
        // 正常に削除完了
        response.status(200).send({
            result: "ok"
        });
        // OGPを削除
        const ogpFilename = `${handleFromId}/${rkeyFromId}`
        await deleteOgp({
            env: envName,
            ogpFilename: ogpFilename
        })
        response.end();

このAPIだいぶ昔に書いたのですが、多分レスポンス自体に意味はなさそうなんですよね...エラーが発生しなかった場合は必ずresponse 200で、{ result: ok }が返却され、それ以外の200レスポンスは無い感じです。
現在のコードではerrorの判定方法が曖昧だったから念の為条件を書いた、というように見えています。過去の自分ならやりそう。

ので、resDeletePage.resultの判定は消しちゃっていいです。おおもとのこのAPIも改善しようと思います。

@ZEKE320
Copy link
Collaborator Author

ZEKE320 commented Apr 11, 2024

@nkte8 返信ありがとうございます!
対応行いましたので、再度レビューをお願いします

Copy link
Owner

@nkte8 nkte8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

対応ありがとうございました!マージします

@nkte8 nkte8 merged commit 6b1ffe1 into develop Apr 12, 2024
1 check passed
@nkte8 nkte8 deleted the preview/issue_58 branch April 12, 2024 14:45
nkte8 added a commit that referenced this pull request Apr 13, 2024
* [Improve code]: pages/* 手動formatter・Linter対応 (#77)

* 🚨 未使用の引数`error`の名前にアンダースコアを追加

* 🚨 テンプレートリテラル内のURL型をstring型に変換

* 🎨 astro/src/pagesにPrettierを適用

* 🎨 astro/src/components/*.astroにPrettierを適用 (#76)

* 🎨 astro/src/layoutsにPrettierを適用 (#80)

* [Improve code]: utils/atproto_api/* 手動formatter・Linter対応 (#81)

* 🚨 astro/src/utils/atproto_apiに`eslint . --fix`を実行

* 🎨 astro/src/utils/atproto_api/models/*にPrettierを適用

* 🎨 astro/src/utils/atproto_api/*にPrettierを適用

* ✏️ searchの誤字を修正

* 🏷️ imageの$typeを"blob"のみに限定

* 🚨 暫定的に、astro/src/utils/atproto_api以下のレスポンスを、as構文で型付け

* 🎨 astro/src/utils/atproto_api/*.tsにPrettierを適用

* 🏷️ uploadBlobの戻り値の型情報を分割

* [Improve code]: astro/src/components/Client/bsky/* 手動formatter・Linter対応 (#83)

* 🚨 astro/src/components/Client/bsky/にESLintの--fixオプションを適用

* 🔒️ target="_blank"付のタグの脆弱性対策を追加

詳細は下記を確認してください。
https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-target-blank.md

* 🚨 readDrafts()が常にtruthyな値を返すようなので、OR演算子を削除

* 🏷️ atproto_apiの型定義変更に伴い、既存の実装の型情報を更新

* 🚨 Promise<void>を返す関数にvoidキーワードを追加

* 🚨 バッククォートで囲われたダブルクォートのエスケープを解除

* ✏️ identifierの綴りを修正

* 🚨 map()で繰り返し出力されるコンポーネントに、key属性を追加

* 🚨 使用されないエラー変数にアンダースコアを追加

* 🏷️ atproto_apiの型定義変更に伴い、既存の実装の型情報を更新

* 🎨 astro/src/components/Client/bskyにPrettierを適用

* ✏️ `LoginForm`の注意文言に、環境変数の`servicename`を用いるよう変更

* 💬 エラー処理後の型アサーションを解説するコメントを追記

* 🥅 使用されていないエラー変数名をアンダースコアのみに変更

* 🚨 エラー型の判定を`"error" in val`形式で統一

* ♻️ 変数定義と値の代入を凝集化

* 🚨 エラー型の判定を`"error" in val`形式で統一

* 🏷️ 型アサーションを用いない実装に変更

* 🔥 不要になったimport文を削除

* 🎨 astro/src/components/Client/bsky/*にPrettierを適用

* ESLint修正漏れの修正

---------

Co-authored-by: nekono <91360587+nkte8@users.noreply.github.com>

* [Improve code]: src/utils/*.ts 手動formatter・Linter対応 (#78)

* 🚨 browser-image-compressionエラー処理のリンティングエラーを修正

* 🎨 astro/src/utils/*.tsにPrettierを適用

* ✏️ searchの綴りを修正

* ✨ useLocalStorage.tsにzodによる型チェックを実装

* 🏷️ Zodオブジェクトをタグ、下書きに各々定義

* ➕ 依存関係にZodを追加

* 🏷️ Zodオブジェクトから型情報を定義 & 実際の使用方法に即した型名に変更

* [Improve code]: lib/* 手動formatter・Linter対応 (#84)

* 🚨 astro/src/libにESLintのfixオプションを適用

* 🚨 Astroの環境変数をstring型とみなす記述を追加

* 🚨 nullish判定を明確化

* 💬 コメントの空白を半角に変更

* 🏷️ 変数の型情報を明記

* 🎨 astro/src/libにPrettierを適用

* ✏️ ファイル名getIdの綴りを修正

* ✏️ ファイル名変更に伴いimport文を修正

* 🏷️ Astro API用の型をZodで再定義

* 🦺 APIレスポンス取得箇所にバリデーションチェックを追加

* 🏷️ モジュール外で型の名前が重複しないよう変更

* 🩹 APIレスポンス処理時の型エラーに関する文言を修正

* 🏷️ API呼び出し箇所の型定義更新に伴い、エラー判定箇所を修正

* 🥅 getOgpMetaエラー時のステータス番号を定義

* ✏️ Zodオブジェクトの命名規則を統一

* 🥅 エラーレスポンスからhtmlを削除

* 🔥 使用されていないレスポンス型定義用のJSONを削除

* 🥅 エラー型判定の実装を改善

* 🥅 エラー型の判定を`"error" in val`形式で統一

* ⚰️ 使用されていない変数を削除

* ⚰️ 不要になった行を削除

* ➕ 依存関係にZodを追加

* 🥅 エラー型の判定ロジックを変更 & 型アサーション削除

* 🔥 不要になったインポート行を削除

* API応答を待機するコードを追加 (#92)

* 🔥 ヘッドブランチを`preview/*`に制限するコードを削除

* 🎨 変数の定義順と使用順を統一

* 💬 分かりやすいコメントに変更

* ✨ ベースブランチに`hotfix/*`が指定できるように修正

---------

Co-authored-by: nekono <91360587+nkte8@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Improve code]: lib/* 手動formatter・Linter対応
2 participants