0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor(core,console): fix useCloudSwr type (#4152)

This commit is contained in:
Gao Sun 2023-07-11 15:25:45 +08:00 committed by GitHub
parent b1c1e03203
commit cee1d0dd20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 48 deletions

View file

@ -26,7 +26,7 @@
"@fontsource/roboto-mono": "^5.0.0",
"@jest/types": "^29.5.0",
"@logto/app-insights": "workspace:^1.3.1",
"@logto/cloud": "0.2.5-31703ea",
"@logto/cloud": "0.2.5-cbbfdc2",
"@logto/connector-kit": "workspace:^1.1.1",
"@logto/core-kit": "workspace:^2.0.1",
"@logto/language-kit": "workspace:^1.0.0",
@ -60,7 +60,7 @@
"@types/react-helmet": "^6.1.6",
"@types/react-modal": "^3.13.1",
"@types/react-syntax-highlighter": "^15.5.1",
"@withtyped/client": "^0.7.11",
"@withtyped/client": "^0.7.17",
"buffer": "^5.7.1",
"classnames": "^2.3.1",
"clean-deep": "^3.4.0",

View file

@ -1,4 +1,9 @@
import useSWR from 'swr';
import {
type GuardedResponse,
type GuardedPayload,
type EmptyPayloadRoutes,
} from '@withtyped/client';
import useSWR, { type SWRResponse } from 'swr';
import { type GetRoutes } from '../types/router';
@ -12,15 +17,18 @@ const normalizeError = (error: unknown) => {
return error instanceof Error ? error : new Error(String(error));
};
/**
* Note: Exclude `/api/services/mails/usage` because it requires a payload.
* Todo: @xiaoyijun Support non-empty payload routes requests for `useCloudSwr` hook (LOG-6513)
*/
type EmptyPayloadGetRoutesKey = Exclude<keyof GetRoutes, '/api/services/mails/usage'>;
export const useCloudSwr = <Key extends EmptyPayloadGetRoutesKey>(key: Key) => {
// The function type signature is mimicked from `ClientRequestHandler`
// in `@withtyped/client` since TypeScript cannot reuse generic type
// alias.
export const useCloudSwr = <T extends keyof GetRoutes>(
...args: T extends EmptyPayloadRoutes<GetRoutes>
? [path: T]
: [path: T, payload: GuardedPayload<GetRoutes[T]>]
): SWRResponse<GuardedResponse<GetRoutes[T]>, Error> => {
const cloudApi = useCloudApi();
const response = useSWR(key, async () => cloudApi.get(key));
const response = useSWR<GuardedResponse<GetRoutes[T]>>(args[0], async () =>
cloudApi.get(...args)
);
// By default, `useSWR()` uses `any` for the error type which is unexpected under our lint rule set.
return { ...response, error: normalizeError(response.error) };

View file

@ -41,7 +41,7 @@
"@logto/shared": "workspace:^2.0.0",
"@logto/ui": "workspace:*",
"@silverhand/essentials": "^2.5.0",
"@withtyped/client": "^0.7.11",
"@withtyped/client": "^0.7.17",
"chalk": "^5.0.0",
"clean-deep": "^3.4.0",
"date-fns": "^2.29.3",
@ -81,7 +81,7 @@
"zod": "^3.20.2"
},
"devDependencies": {
"@logto/cloud": "0.2.5-1a68662",
"@logto/cloud": "0.2.5-cbbfdc2",
"@silverhand/eslint-config": "4.0.1",
"@silverhand/ts-config": "4.0.0",
"@types/debug": "^4.1.7",

View file

@ -86,7 +86,7 @@
"@logto/phrases": "workspace:^1.4.1",
"@logto/phrases-ui": "workspace:^1.2.0",
"@logto/shared": "workspace:^2.0.0",
"@withtyped/server": "^0.12.0"
"@withtyped/server": "^0.12.5"
},
"peerDependencies": {
"zod": "^3.20.2"

View file

@ -2755,8 +2755,8 @@ importers:
specifier: workspace:^1.3.1
version: link:../app-insights
'@logto/cloud':
specifier: 0.2.5-31703ea
version: 0.2.5-31703ea(zod@3.20.2)
specifier: 0.2.5-cbbfdc2
version: 0.2.5-cbbfdc2(zod@3.20.2)
'@logto/connector-kit':
specifier: workspace:^1.1.1
version: link:../toolkit/connector-kit
@ -2857,8 +2857,8 @@ importers:
specifier: ^15.5.1
version: 15.5.1
'@withtyped/client':
specifier: ^0.7.11
version: 0.7.11(zod@3.20.2)
specifier: ^0.7.17
version: 0.7.17(zod@3.20.2)
buffer:
specifier: ^5.7.1
version: 5.7.1
@ -3079,8 +3079,8 @@ importers:
specifier: ^2.5.0
version: 2.5.0
'@withtyped/client':
specifier: ^0.7.11
version: 0.7.11(zod@3.20.2)
specifier: ^0.7.17
version: 0.7.17(zod@3.20.2)
chalk:
specifier: ^5.0.0
version: 5.1.2
@ -3194,8 +3194,8 @@ importers:
version: 3.20.2
devDependencies:
'@logto/cloud':
specifier: 0.2.5-1a68662
version: 0.2.5-1a68662(zod@3.20.2)
specifier: 0.2.5-cbbfdc2
version: 0.2.5-cbbfdc2(zod@3.20.2)
'@silverhand/eslint-config':
specifier: 4.0.1
version: 4.0.1(eslint@8.44.0)(prettier@3.0.0)(typescript@5.0.2)
@ -3547,8 +3547,8 @@ importers:
specifier: workspace:^2.0.0
version: link:../shared
'@withtyped/server':
specifier: ^0.12.0
version: 0.12.0(zod@3.20.2)
specifier: ^0.12.5
version: 0.12.5(zod@3.20.2)
zod:
specifier: ^3.20.2
version: 3.20.2
@ -7171,7 +7171,7 @@ packages:
resolution: {integrity: sha512-vw8xDW8k38/58Q1r592z/9JdsmUh4+LMmoVm/Nu7LbWKlT32eD3H9hZDkFK9XEHpriifhI0hP7asGWEmhrEUuQ==}
dependencies:
'@logto/js': 2.1.1
'@silverhand/essentials': 2.6.2
'@silverhand/essentials': 2.7.0
camelcase-keys: 7.0.2
jose: 4.14.4
dev: true
@ -7180,25 +7180,17 @@ packages:
resolution: {integrity: sha512-7I2ELo5UWIJsFCYK/gX465l0+QhXTdyYWkgb2CcdPu5KbaPBNpASedm+fEV2NREYe2svbNODFhog6UMA/xGQnQ==}
dependencies:
'@logto/js': 2.1.1
'@silverhand/essentials': 2.6.2
'@silverhand/essentials': 2.7.0
camelcase-keys: 7.0.2
jose: 4.14.4
dev: true
/@logto/cloud@0.2.5-1a68662(zod@3.20.2):
resolution: {integrity: sha512-lmJiFO0cFGurg/B1dlebeyMMN8jIx7lALyqwATcAi1m902l6L92iN5DRkojnNOVw8d2XQ8zzeOyW+9I/Jew5Uw==}
/@logto/cloud@0.2.5-cbbfdc2(zod@3.20.2):
resolution: {integrity: sha512-qul8lxkbAczEHJSyramQJW3BaNAdJQHlryIzTAHpcWpNGZogP3aacJuF0neBnnn8mJqAWy1rIGAGnb4d16fEFQ==}
engines: {node: ^18.12.0}
dependencies:
'@withtyped/server': 0.12.0(zod@3.20.2)
transitivePeerDependencies:
- zod
dev: true
/@logto/cloud@0.2.5-31703ea(zod@3.20.2):
resolution: {integrity: sha512-d0UfWpm5djW8+pEA4LJnJg9yDUebwcdxll7YLJGCPeo0SxUaWwHeef3yfmlKavx6Q4LCkVLY1NinQg+WBLW9Xw==}
engines: {node: ^18.12.0}
dependencies:
'@withtyped/server': 0.12.0(zod@3.20.2)
'@silverhand/essentials': 2.7.0
'@withtyped/server': 0.12.5(zod@3.20.2)
transitivePeerDependencies:
- zod
dev: true
@ -8733,6 +8725,11 @@ packages:
/@silverhand/essentials@2.6.2:
resolution: {integrity: sha512-1b5u2BGEa14V3o8XzaE7eL+nuwmQe8c1wqSMcGvq+KAusPPZo9tV4glbfF16Xi/ohv37vUpBGJ2DNf4CfuxBLw==}
engines: {node: ^16.13.0 || ^18.12.0 || ^19.2.0, pnpm: ^8.0.0}
dev: true
/@silverhand/essentials@2.7.0:
resolution: {integrity: sha512-F5Qo5ZNnERUURK/9F1ZIi4FBDM22aeD59Zv0VtkgIhUL9tYK9svA2Jz88NNdYBwqCPrh8ExZlpFNi+pNmXKNlQ==}
engines: {node: ^16.13.0 || ^18.12.0 || ^19.2.0, pnpm: ^8.0.0}
/@silverhand/ts-config-react@4.0.0(typescript@5.0.2):
resolution: {integrity: sha512-IkZka1iuIBgw0AUbsknghw1vOIs4zOgUxR8jL38Kuk63hmSj687N4BWBb8KhVMhqaG4U/DYkbSEZuwsyHBe68g==}
@ -9812,25 +9809,25 @@ packages:
eslint-visitor-keys: 3.4.1
dev: true
/@withtyped/client@0.7.11(zod@3.20.2):
resolution: {integrity: sha512-scFmj4O7R83KlIEsjWEZ9QoxQ3D0k5LD/+JQ/zOk8Wg7iXPIDs9A/j/Loea4UCuNTkLPZR9Sgfho9PBnyqNnsg==}
/@withtyped/client@0.7.17(zod@3.20.2):
resolution: {integrity: sha512-D8kwJBKryALjNcjHRLyARRTVnlGU+iUwJ1AdDsoSv+Sum17GDxi0s4F5DYh97bfSmdn/K4SGFBI2e0MdHPP+mg==}
dependencies:
'@withtyped/server': 0.12.0(zod@3.20.2)
'@withtyped/shared': 0.2.1
'@withtyped/server': 0.12.5(zod@3.20.2)
'@withtyped/shared': 0.2.2
transitivePeerDependencies:
- zod
/@withtyped/server@0.12.0(zod@3.20.2):
resolution: {integrity: sha512-u5Qe+gr1kK/5CJi7NKf2iIQkbXlxhPXdDYqc7IeoMn0QHGn1hSkB9G3FB6gtx7kI28LY1gSUii4CJf7vX40PZw==}
/@withtyped/server@0.12.5(zod@3.20.2):
resolution: {integrity: sha512-mKDPGCJzh0xna4Vi2zVsHg/ZfMNQHDV3+jvsral2PJdRzI8qU7au7I/ytU6Vr6BU1fFESkNrIuUpCmyfBAwX7g==}
peerDependencies:
zod: ^3.19.1
dependencies:
'@silverhand/essentials': 2.6.2
'@withtyped/shared': 0.2.1
'@silverhand/essentials': 2.7.0
'@withtyped/shared': 0.2.2
zod: 3.20.2
/@withtyped/shared@0.2.1:
resolution: {integrity: sha512-5Cfcku3rgzQJ4rjdv7SHonD7KPIsLzFItw/szOdGbUl4eiJQ9sH42L87e81W2w/vTecPEaZKztGrUCd3RFDILQ==}
/@withtyped/shared@0.2.2:
resolution: {integrity: sha512-Vpcj12NqaoZ8M5Z/1kffheI9FBZEm9goed0THmgTcMKXLHjXSRbMZMp0olVxovEgaTIAydshqJOQUXKZMctIZw==}
/@xmldom/xmldom@0.8.7:
resolution: {integrity: sha512-sI1Ly2cODlWStkINzqGrZ8K6n+MTSbAeQnAipGyL+KZCXuHaRlj2gyyy8B/9MvsFFqN7XHryQnB2QwhzvJXovg==}