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

feat(rest): add wrapper client #3193

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Conversation

shabaraba
Copy link
Member

@shabaraba shabaraba commented Feb 21, 2025

Why

We want to improve the development experience using rest.js.

What

  • Add a wrapper method of createClient() to reduce the code and improve the developer experience.
  • Change the arguments of createClient() method similar to those of rest-api-client.
  • Add a method of api() to the client, whose interface is similar to kintone.api() of JavaScript API
  • Update README and migration-guide

How to test

Checklist

  • Read CONTRIBUTING.md
  • Updated documentation if it is required.
  • Added tests if it is required.
  • Passed pnpm lint and pnpm test on the root directory.

@shabaraba shabaraba requested a review from a team as a code owner February 21, 2025 07:50
@shabaraba shabaraba requested review from chihiro-adachi and tasshi-me and removed request for a team February 21, 2025 07:50
@shabaraba shabaraba force-pushed the feat/add-wrapper-client branch from a700b27 to e65d208 Compare February 21, 2025 07:56
Comment on lines 14 to 20
auth?: AuthOption;
basicAuth?: BasicAuthOption;
proxy?: Proxy;
httpsAgent?: HttpsAgent;
clientCertAuth?: ClientCertAuth;
userAgent?: string;
socketTimeout?: number;
Copy link
Member

Choose a reason for hiding this comment

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

この辺りは OR | AND の関係性のオプションがあれば、それを型に反映したいです。
例えば httpsAgent はいくつかのHTTP or TCPレイヤーの設定と排他になっているので、それを反映してもらうと良さそうです。

Copy link
Member Author

Choose a reason for hiding this comment

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

@tasshi-me
書き方が適切か見ていただきたいのですが、proxy, httpsAgent, clientCertAuthを1つだけしか指定できないようにしました。

Copy link
Member

Choose a reason for hiding this comment

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

あーなるほど、
単純に↓のようなunion typeにしても余剰プロパティチェックが効かないのでneverを入れてるんですね。

    | {
        proxy?: Proxy;
      }
    | {
        httpsAgent?: HttpsAgent;
      }
    | {
        certAuth?: ClientCertAuth;
      }

型補完が分かりにくくなるのでやはりdiscriminated unionかなーとも思いましたが、利用者が直接使うオプションなのでkindsを指定させるほうがややこしそう。
これで良さそうです!

Comment on lines 1 to 14
export type ApiTokenAuth = {
apiToken: string | string[];
};

export type PasswordAuth = {
username: string;
password: string;
};

export type SessionAuth = {};

export type OAuthTokenAuth = {
oAuthToken: string;
};
Copy link
Member

Choose a reason for hiding this comment

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

ここは discriminated union でもいいかもしれないですが、どっちでも良さそう。
(プロパティ名で判定できているし、認証の必要情報が大きく変わることはなさそう)

Copy link
Member Author

Choose a reason for hiding this comment

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

こちらはdiscriminated unionに変更しました。

@shabaraba shabaraba force-pushed the feat/add-wrapper-client branch 2 times, most recently from 48485eb to eb187e7 Compare February 27, 2025 07:58
@shabaraba shabaraba force-pushed the feat/add-wrapper-client branch from eb187e7 to a721a3c Compare February 27, 2025 08:17
Comment on lines +14 to +16
export const createClient = (clientOptions: KintoneClientOptions) => {
return _createClient<paths>(clientOptions);
};
Copy link
Member Author

Choose a reason for hiding this comment

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

pathsが巨大すぎるので、_createClientをラップせずpathsをジェネリクスで渡さず直接コーディングすると、型が壊れてしまう。

Comment on lines +19 to +34
} & (
| {
proxy?: Proxy;
httpsAgent?: never;
certAuth?: never;
}
| {
proxy?: never;
httpsAgent?: HttpsAgent;
certAuth?: never;
}
| {
proxy?: never;
httpsAgent?: never;
certAuth?: ClientCertAuth;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

never型を指定することでどれか一つしかとれないようにした

[key: string]: unknown;
};

type KintoneMethodType = Extract<HttpMethod, "get" | "post" | "put" | "delete">;
Copy link
Member Author

Choose a reason for hiding this comment

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

Extractを使えばHttpMethodの一部であることを明示できる、多分

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.

2 participants