-
Notifications
You must be signed in to change notification settings - Fork 1
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
フロントエンドのデータ取得の流れを整備 #476
Merged
Merged
フロントエンドのデータ取得の流れを整備 #476
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e845f58
fix: back to story on storybook
tosuke a377498
feat: use async data on account menu
tosuke 136fe74
feat: fetch current user from API
tosuke 4e67ddc
test: test connect RPC w/ MSW
tosuke 436d4f8
feat: test on CI
tosuke 0149418
Merge remote-tracking branch 'origin/main' into frontend-connect
tosuke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
frontend/packages/contestant/app/__test__/msw/connect.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { | ||
type RequestHandlerOptions, | ||
type ResponseResolver, | ||
RequestHandler, | ||
} from "msw"; | ||
import { | ||
type UniversalHandler, | ||
universalServerRequestFromFetch, | ||
universalServerResponseToFetch, | ||
} from "@connectrpc/connect/protocol"; | ||
import { | ||
type ConnectRouter, | ||
type MethodImpl, | ||
ServiceImpl, | ||
createConnectRouter, | ||
} from "@connectrpc/connect"; | ||
import type { DescMethod, DescService } from "@bufbuild/protobuf"; | ||
|
||
export type ConnectHandlerInfo = { | ||
header: string; | ||
kind: "service" | "rpc"; | ||
name: string; | ||
}; | ||
|
||
type ConnectRequestParsedResult = { | ||
handler: UniversalHandler | undefined; | ||
}; | ||
|
||
export type ConnectResolverExtras = { | ||
handler: UniversalHandler; | ||
}; | ||
|
||
export type ConnectResponseResolver = ResponseResolver<ConnectResolverExtras>; | ||
|
||
export type ConnectHandlerOptions = RequestHandlerOptions; | ||
const defaultConnectResolver: ConnectResponseResolver = async ({ | ||
request, | ||
handler, | ||
}) => { | ||
const ureq = universalServerRequestFromFetch(request.clone(), {}); | ||
const ures = await handler(ureq); | ||
return universalServerResponseToFetch(ures); | ||
}; | ||
|
||
export class ConnectHandler extends RequestHandler< | ||
ConnectHandlerInfo, | ||
ConnectRequestParsedResult, | ||
ConnectResolverExtras | ||
> { | ||
#router: ConnectRouter; | ||
constructor( | ||
info: Omit<ConnectHandlerInfo, "header">, | ||
routes: (router: ConnectRouter) => void, | ||
options?: ConnectHandlerOptions, | ||
) { | ||
super({ | ||
info: { | ||
...info, | ||
header: `${info.kind} ${info.name}`, | ||
}, | ||
resolver: defaultConnectResolver, | ||
options, | ||
}); | ||
|
||
const router = createConnectRouter(); | ||
routes(router); | ||
this.#router = router; | ||
} | ||
|
||
#handler(req: Request): UniversalHandler | undefined { | ||
const url = new URL(req.url); | ||
const handler = this.#router.handlers.find( | ||
(h) => h.requestPath == url.pathname, | ||
); | ||
if (handler == null) { | ||
return; | ||
} | ||
if (!handler.allowedMethods.includes(req.method)) { | ||
return; | ||
} | ||
return handler; | ||
} | ||
|
||
parse({ request }: { request: Request }) { | ||
return Promise.resolve({ | ||
handler: this.#handler(request), | ||
}); | ||
} | ||
|
||
predicate({ | ||
parsedResult: { handler }, | ||
}: { | ||
request: Request; | ||
parsedResult: ConnectRequestParsedResult; | ||
}) { | ||
if (handler == null) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
extendResolverArgs({ | ||
parsedResult: { handler }, | ||
}: { | ||
request: Request; | ||
parsedResult: ConnectRequestParsedResult; | ||
}) { | ||
return { | ||
handler: handler!, | ||
}; | ||
} | ||
|
||
log({ | ||
request, | ||
response, | ||
parsedResult: { handler }, | ||
}: { | ||
request: Request; | ||
response: Response; | ||
parsedResult: ConnectRequestParsedResult; | ||
}): void { | ||
if (handler == null) { | ||
throw new Error("handler is null"); | ||
} | ||
const method = handler.method; | ||
|
||
console.groupCollapsed( | ||
`${method.methodKind} ${method.parent.typeName}/${method.name} (${response.status} ${response.statusText})`, | ||
); | ||
console.log("Request:", { | ||
url: new URL(request.url), | ||
method: request.method, | ||
headers: Object.fromEntries(request.headers.entries()), | ||
}); | ||
console.log("Response:", { | ||
status: response.status, | ||
statusText: response.statusText, | ||
headers: Object.fromEntries(response.headers.entries()), | ||
}); | ||
console.groupEnd(); | ||
} | ||
} | ||
|
||
export const connect = { | ||
rpc: <M extends DescMethod>( | ||
method: M, | ||
impl: MethodImpl<M>, | ||
options?: ConnectHandlerOptions, | ||
) => | ||
new ConnectHandler( | ||
{ kind: method.kind, name: `${method.parent.typeName}/${method.name}` }, | ||
(router) => router.rpc(method, impl), | ||
options, | ||
), | ||
service: <S extends DescService>( | ||
service: S, | ||
impl: ServiceImpl<S>, | ||
options?: ConnectHandlerOptions, | ||
) => | ||
new ConnectHandler( | ||
{ kind: "service", name: service.typeName }, | ||
(router) => router.service(service, impl), | ||
options, | ||
), | ||
}; | ||
Comment on lines
+144
to
+165
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [zatsu] なぜかなくて人々が困っている様子が見える(connectrpc/connect-es#825) のでライブラリとして外に切り出してもいいかもね There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 自分のリポジトリで公開して、それを利用する形にしてしまって良いのでは |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { afterAll, afterEach, beforeAll } from "vitest"; | ||
import type { RequestHandler, SharedOptions } from "msw"; | ||
import { setupServer } from "msw/node"; | ||
|
||
export function setupMSW( | ||
handlers: readonly RequestHandler[] = [], | ||
options: Partial<SharedOptions> = { onUnhandledRequest: "error" }, | ||
) { | ||
const server = setupServer(...handlers); | ||
beforeAll(() => server.listen(options)); | ||
afterAll(() => server.close()); | ||
afterEach(() => server.resetHandlers()); | ||
return server; | ||
} |
31 changes: 23 additions & 8 deletions
31
frontend/packages/contestant/app/components/app-shell/account-menu.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 9 additions & 4 deletions
13
frontend/packages/contestant/app/components/app-shell/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自分の中ではフロントエンドのビルドもCIで確認するようなイメージがあるんだけど、それはなくても大丈夫?