chore: display apiState

Other changes:

* fix close button overflow

* minor responsive style tweaks

* display reducerPath in query tab
This commit is contained in:
FaberVitale 2021-06-20 10:57:52 +02:00
parent bcb493cb0c
commit b8ee704060
7 changed files with 109 additions and 104 deletions

View File

@ -7,116 +7,116 @@
/* eslint-disable */ /* eslint-disable */
/* tslint:disable */ /* tslint:disable */
const INTEGRITY_CHECKSUM = '82ef9b96d8393b6da34527d1d6e19187'; const INTEGRITY_CHECKSUM = '82ef9b96d8393b6da34527d1d6e19187'
const bypassHeaderName = 'x-msw-bypass'; const bypassHeaderName = 'x-msw-bypass'
const activeClientIds = new Set(); const activeClientIds = new Set()
self.addEventListener('install', function () { self.addEventListener('install', function () {
return self.skipWaiting(); return self.skipWaiting()
}); })
self.addEventListener('activate', async function (event) { self.addEventListener('activate', async function (event) {
return self.clients.claim(); return self.clients.claim()
}); })
self.addEventListener('message', async function (event) { self.addEventListener('message', async function (event) {
const clientId = event.source.id; const clientId = event.source.id
if (!clientId || !self.clients) { if (!clientId || !self.clients) {
return; return
} }
const client = await self.clients.get(clientId); const client = await self.clients.get(clientId)
if (!client) { if (!client) {
return; return
} }
const allClients = await self.clients.matchAll(); const allClients = await self.clients.matchAll()
switch (event.data) { switch (event.data) {
case 'KEEPALIVE_REQUEST': { case 'KEEPALIVE_REQUEST': {
sendToClient(client, { sendToClient(client, {
type: 'KEEPALIVE_RESPONSE', type: 'KEEPALIVE_RESPONSE',
}); })
break; break
} }
case 'INTEGRITY_CHECK_REQUEST': { case 'INTEGRITY_CHECK_REQUEST': {
sendToClient(client, { sendToClient(client, {
type: 'INTEGRITY_CHECK_RESPONSE', type: 'INTEGRITY_CHECK_RESPONSE',
payload: INTEGRITY_CHECKSUM, payload: INTEGRITY_CHECKSUM,
}); })
break; break
} }
case 'MOCK_ACTIVATE': { case 'MOCK_ACTIVATE': {
activeClientIds.add(clientId); activeClientIds.add(clientId)
sendToClient(client, { sendToClient(client, {
type: 'MOCKING_ENABLED', type: 'MOCKING_ENABLED',
payload: true, payload: true,
}); })
break; break
} }
case 'MOCK_DEACTIVATE': { case 'MOCK_DEACTIVATE': {
activeClientIds.delete(clientId); activeClientIds.delete(clientId)
break; break
} }
case 'CLIENT_CLOSED': { case 'CLIENT_CLOSED': {
activeClientIds.delete(clientId); activeClientIds.delete(clientId)
const remainingClients = allClients.filter((client) => { const remainingClients = allClients.filter((client) => {
return client.id !== clientId; return client.id !== clientId
}); })
// Unregister itself when there are no more clients // Unregister itself when there are no more clients
if (remainingClients.length === 0) { if (remainingClients.length === 0) {
self.registration.unregister(); self.registration.unregister()
} }
break; break
} }
} }
}); })
// Resolve the "master" client for the given event. // Resolve the "master" client for the given event.
// Client that issues a request doesn't necessarily equal the client // Client that issues a request doesn't necessarily equal the client
// that registered the worker. It's with the latter the worker should // that registered the worker. It's with the latter the worker should
// communicate with during the response resolving phase. // communicate with during the response resolving phase.
async function resolveMasterClient(event) { async function resolveMasterClient(event) {
const client = await self.clients.get(event.clientId); const client = await self.clients.get(event.clientId)
if (client.frameType === 'top-level') { if (client.frameType === 'top-level') {
return client; return client
} }
const allClients = await self.clients.matchAll(); const allClients = await self.clients.matchAll()
return allClients return allClients
.filter((client) => { .filter((client) => {
// Get only those clients that are currently visible. // Get only those clients that are currently visible.
return client.visibilityState === 'visible'; return client.visibilityState === 'visible'
}) })
.find((client) => { .find((client) => {
// Find the client ID that's recorded in the // Find the client ID that's recorded in the
// set of clients that have registered the worker. // set of clients that have registered the worker.
return activeClientIds.has(client.id); return activeClientIds.has(client.id)
}); })
} }
async function handleRequest(event, requestId) { async function handleRequest(event, requestId) {
const client = await resolveMasterClient(event); const client = await resolveMasterClient(event)
const response = await getResponse(event, client, requestId); const response = await getResponse(event, client, requestId)
// Send back the response clone for the "response:*" life-cycle events. // Send back the response clone for the "response:*" life-cycle events.
// Ensure MSW is active and ready to handle the message, otherwise // Ensure MSW is active and ready to handle the message, otherwise
// this message will pend indefinitely. // this message will pend indefinitely.
if (client && activeClientIds.has(client.id)) { if (client && activeClientIds.has(client.id)) {
(async function () { ;(async function () {
const clonedResponse = response.clone(); const clonedResponse = response.clone()
sendToClient(client, { sendToClient(client, {
type: 'RESPONSE', type: 'RESPONSE',
payload: { payload: {
@ -130,21 +130,21 @@ async function handleRequest(event, requestId) {
headers: serializeHeaders(clonedResponse.headers), headers: serializeHeaders(clonedResponse.headers),
redirected: clonedResponse.redirected, redirected: clonedResponse.redirected,
}, },
}); })
})(); })()
} }
return response; return response
} }
async function getResponse(event, client, requestId) { async function getResponse(event, client, requestId) {
const { request } = event; const { request } = event
const requestClone = request.clone(); const requestClone = request.clone()
const getOriginalResponse = () => fetch(requestClone); const getOriginalResponse = () => fetch(requestClone)
// Bypass mocking when the request client is not active. // Bypass mocking when the request client is not active.
if (!client) { if (!client) {
return getOriginalResponse(); return getOriginalResponse()
} }
// Bypass initial page load requests (i.e. static assets). // Bypass initial page load requests (i.e. static assets).
@ -152,26 +152,26 @@ async function getResponse(event, client, requestId) {
// means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet // means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet
// and is not ready to handle requests. // and is not ready to handle requests.
if (!activeClientIds.has(client.id)) { if (!activeClientIds.has(client.id)) {
return await getOriginalResponse(); return await getOriginalResponse()
} }
// Bypass requests with the explicit bypass header // Bypass requests with the explicit bypass header
if (requestClone.headers.get(bypassHeaderName) === 'true') { if (requestClone.headers.get(bypassHeaderName) === 'true') {
const cleanRequestHeaders = serializeHeaders(requestClone.headers); const cleanRequestHeaders = serializeHeaders(requestClone.headers)
// Remove the bypass header to comply with the CORS preflight check. // Remove the bypass header to comply with the CORS preflight check.
delete cleanRequestHeaders[bypassHeaderName]; delete cleanRequestHeaders[bypassHeaderName]
const originalRequest = new Request(requestClone, { const originalRequest = new Request(requestClone, {
headers: new Headers(cleanRequestHeaders), headers: new Headers(cleanRequestHeaders),
}); })
return fetch(originalRequest); return fetch(originalRequest)
} }
// Send the request to the client-side MSW. // Send the request to the client-side MSW.
const reqHeaders = serializeHeaders(request.headers); const reqHeaders = serializeHeaders(request.headers)
const body = await request.text(); const body = await request.text()
const clientMessage = await sendToClient(client, { const clientMessage = await sendToClient(client, {
type: 'REQUEST', type: 'REQUEST',
@ -192,31 +192,31 @@ async function getResponse(event, client, requestId) {
bodyUsed: request.bodyUsed, bodyUsed: request.bodyUsed,
keepalive: request.keepalive, keepalive: request.keepalive,
}, },
}); })
switch (clientMessage.type) { switch (clientMessage.type) {
case 'MOCK_SUCCESS': { case 'MOCK_SUCCESS': {
return delayPromise( return delayPromise(
() => respondWithMock(clientMessage), () => respondWithMock(clientMessage),
clientMessage.payload.delay clientMessage.payload.delay,
); )
} }
case 'MOCK_NOT_FOUND': { case 'MOCK_NOT_FOUND': {
return getOriginalResponse(); return getOriginalResponse()
} }
case 'NETWORK_ERROR': { case 'NETWORK_ERROR': {
const { name, message } = clientMessage.payload; const { name, message } = clientMessage.payload
const networkError = new Error(message); const networkError = new Error(message)
networkError.name = name; networkError.name = name
// Rejecting a request Promise emulates a network error. // Rejecting a request Promise emulates a network error.
throw networkError; throw networkError
} }
case 'INTERNAL_ERROR': { case 'INTERNAL_ERROR': {
const parsedBody = JSON.parse(clientMessage.payload.body); const parsedBody = JSON.parse(clientMessage.payload.body)
console.error( console.error(
`\ `\
@ -229,38 +229,38 @@ This exception has been gracefully handled as a 500 response, however, it's stro
If you wish to mock an error response, please refer to this guide: https://mswjs.io/docs/recipes/mocking-error-responses\ If you wish to mock an error response, please refer to this guide: https://mswjs.io/docs/recipes/mocking-error-responses\
`, `,
request.method, request.method,
request.url request.url,
); )
return respondWithMock(clientMessage); return respondWithMock(clientMessage)
} }
} }
return getOriginalResponse(); return getOriginalResponse()
} }
self.addEventListener('fetch', function (event) { self.addEventListener('fetch', function (event) {
const { request } = event; const { request } = event
// Bypass navigation requests. // Bypass navigation requests.
if (request.mode === 'navigate') { if (request.mode === 'navigate') {
return; return
} }
// Opening the DevTools triggers the "only-if-cached" request // Opening the DevTools triggers the "only-if-cached" request
// that cannot be handled by the worker. Bypass such requests. // that cannot be handled by the worker. Bypass such requests.
if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') { if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') {
return; return
} }
// Bypass all requests when there are no active clients. // Bypass all requests when there are no active clients.
// Prevents the self-unregistered worked from handling requests // Prevents the self-unregistered worked from handling requests
// after it's been deleted (still remains active until the next reload). // after it's been deleted (still remains active until the next reload).
if (activeClientIds.size === 0) { if (activeClientIds.size === 0) {
return; return
} }
const requestId = uuidv4(); const requestId = uuidv4()
return event.respondWith( return event.respondWith(
handleRequest(event, requestId).catch((error) => { handleRequest(event, requestId).catch((error) => {
@ -268,55 +268,55 @@ self.addEventListener('fetch', function (event) {
'[MSW] Failed to mock a "%s" request to "%s": %s', '[MSW] Failed to mock a "%s" request to "%s": %s',
request.method, request.method,
request.url, request.url,
error error,
); )
}) }),
); )
}); })
function serializeHeaders(headers) { function serializeHeaders(headers) {
const reqHeaders = {}; const reqHeaders = {}
headers.forEach((value, name) => { headers.forEach((value, name) => {
reqHeaders[name] = reqHeaders[name] reqHeaders[name] = reqHeaders[name]
? [].concat(reqHeaders[name]).concat(value) ? [].concat(reqHeaders[name]).concat(value)
: value; : value
}); })
return reqHeaders; return reqHeaders
} }
function sendToClient(client, message) { function sendToClient(client, message) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const channel = new MessageChannel(); const channel = new MessageChannel()
channel.port1.onmessage = (event) => { channel.port1.onmessage = (event) => {
if (event.data && event.data.error) { if (event.data && event.data.error) {
return reject(event.data.error); return reject(event.data.error)
} }
resolve(event.data); resolve(event.data)
}; }
client.postMessage(JSON.stringify(message), [channel.port2]); client.postMessage(JSON.stringify(message), [channel.port2])
}); })
} }
function delayPromise(cb, duration) { function delayPromise(cb, duration) {
return new Promise((resolve) => { return new Promise((resolve) => {
setTimeout(() => resolve(cb()), duration); setTimeout(() => resolve(cb()), duration)
}); })
} }
function respondWithMock(clientMessage) { function respondWithMock(clientMessage) {
return new Response(clientMessage.payload.body, { return new Response(clientMessage.payload.body, {
...clientMessage.payload, ...clientMessage.payload,
headers: clientMessage.payload.headers, headers: clientMessage.payload.headers,
}); })
} }
function uuidv4() { function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0; const r = (Math.random() * 16) | 0
const v = c == 'x' ? r : (r & 0x3) | 0x8; const v = c == 'x' ? r : (r & 0x3) | 0x8
return v.toString(16); return v.toString(16)
}); })
} }

View File

@ -57,7 +57,7 @@ class RtkQueryInspector<S, A extends Action<unknown>> extends Component<
}; };
} }
static wideLayout = 650; static wideLayout = 600;
static getDerivedStateFromProps( static getDerivedStateFromProps(
props: RtkQueryInspectorProps<unknown, Action<unknown>>, props: RtkQueryInspectorProps<unknown, Action<unknown>>,
@ -164,7 +164,7 @@ class RtkQueryInspector<S, A extends Action<unknown>> extends Component<
styling={styling} styling={styling}
tags={currentTags} tags={currentTags}
querySubscriptions={currentQuerySubscriptions} querySubscriptions={currentQuerySubscriptions}
apiConfig={currentRtkApi?.config ?? null} apiState={currentRtkApi}
isWideLayout={isWideLayout} isWideLayout={isWideLayout}
apiStats={currentApiStats} apiStats={currentApiStats}
/> />

View File

@ -90,7 +90,7 @@ export class QueryPreview extends React.PureComponent<QueryPreviewProps> {
queryInfo, queryInfo,
isWideLayout, isWideLayout,
selectedTab, selectedTab,
apiConfig, apiState,
onTabChange, onTabChange,
querySubscriptions, querySubscriptions,
tags, tags,
@ -135,7 +135,7 @@ export class QueryPreview extends React.PureComponent<QueryPreviewProps> {
querySubscriptions={querySubscriptions} querySubscriptions={querySubscriptions}
queryInfo={queryInfo} queryInfo={queryInfo}
tags={tags} tags={tags}
apiConfig={apiConfig} apiState={apiState}
isWideLayout={isWideLayout} isWideLayout={isWideLayout}
apiStats={apiStats} apiStats={apiStats}
/> />

View File

@ -4,16 +4,21 @@ import { QueryPreviewTabProps } from '../types';
import { TreeView } from './TreeView'; import { TreeView } from './TreeView';
interface TreeDisplayed { interface TreeDisplayed {
config: QueryPreviewTabProps['apiConfig']; reducerPath: string;
api: QueryPreviewTabProps['apiState'];
stats: QueryPreviewTabProps['apiStats']; stats: QueryPreviewTabProps['apiStats'];
} }
export class QueryPreviewApi extends PureComponent<QueryPreviewTabProps> { export class QueryPreviewApi extends PureComponent<QueryPreviewTabProps> {
selectData = createSelector( selectData = createSelector(
[ [
({ apiConfig }: QueryPreviewTabProps) => apiConfig, ({ apiState }: QueryPreviewTabProps) => apiState,
({ apiStats }: QueryPreviewTabProps) => apiStats, ({ apiStats }: QueryPreviewTabProps) => apiStats,
], ],
(apiConfig, apiStats) => ({ config: apiConfig, stats: apiStats }) (apiState, apiStats) => ({
reducerPath: apiState?.config?.reducerPath ?? null,
apiState: apiState,
stats: apiStats,
})
); );
render(): ReactNode { render(): ReactNode {

View File

@ -17,6 +17,7 @@ type ComputedQueryInfo = {
interface FormattedQuery extends ComputedQueryInfo { interface FormattedQuery extends ComputedQueryInfo {
queryKey: string; queryKey: string;
reducerPath: string;
statusFlags: RTKStatusFlags; statusFlags: RTKStatusFlags;
query: RtkQueryState; query: RtkQueryState;
} }
@ -32,7 +33,7 @@ export class QueryPreviewInfo extends PureComponent<QueryPreviewTabProps> {
return null; return null;
} }
const { query, queryKey } = queryInfo; const { query, queryKey, reducerPath } = queryInfo;
const startedAt = query.startedTimeStamp const startedAt = query.startedTimeStamp
? new Date(query.startedTimeStamp).toISOString() ? new Date(query.startedTimeStamp).toISOString()
@ -46,6 +47,7 @@ export class QueryPreviewInfo extends PureComponent<QueryPreviewTabProps> {
return { return {
queryKey, queryKey,
reducerPath,
startedAt, startedAt,
latestFetchAt, latestFetchAt,
statusFlags, statusFlags,

View File

@ -84,7 +84,7 @@ const getSheetFromColorMap = (map: ColorMap) => {
'&[data-wide-layout="1"]': { '&[data-wide-layout="1"]': {
height: '100%', height: '100%',
width: '40%', width: '45%',
borderRightWidth: 1, borderRightWidth: 1,
borderStyle: 'solid', borderStyle: 'solid',
}, },
@ -241,7 +241,7 @@ const getSheetFromColorMap = (map: ColorMap) => {
alignItems: 'center', alignItems: 'center',
flexFlow: 'row nowrap', flexFlow: 'row nowrap',
flex: '1 1 auto', flex: '1 1 auto',
paddingRight: 6,
'& input': { '& input': {
outline: 'none', outline: 'none',
border: 'none', border: 'none',
@ -283,8 +283,6 @@ const getSheetFromColorMap = (map: ColorMap) => {
content: '"\u00d7"', content: '"\u00d7"',
display: 'block', display: 'block',
padding: 4, padding: 4,
width: '16px',
height: '16px',
fontSize: '16px', fontSize: '16px',
color: map.TEXT_COLOR, color: map.TEXT_COLOR,
background: 'transparent', background: 'transparent',

View File

@ -130,7 +130,7 @@ export interface ApiStats {
export interface QueryPreviewTabProps extends StyleUtils { export interface QueryPreviewTabProps extends StyleUtils {
queryInfo: QueryInfo | null; queryInfo: QueryInfo | null;
apiConfig: RtkQueryApiState['config'] | null; apiState: RtkQueryApiState | null;
querySubscriptions: RTKQuerySubscribers | null; querySubscriptions: RTKQuerySubscribers | null;
isWideLayout: boolean; isWideLayout: boolean;
tags: RtkQueryTag[]; tags: RtkQueryTag[];