-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(migrate): limit client methods during migration
- Loading branch information
Showing
5 changed files
with
78 additions
and
12 deletions.
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
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
65 changes: 65 additions & 0 deletions
65
packages/@sanity/migrate/src/runner/utils/createContextClient.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,65 @@ | ||
import {createClient, type SanityClient} from '@sanity/client' | ||
import {limitClientConcurrency} from './limitClientConcurrency' | ||
|
||
export function createContextClient(config: Parameters<typeof createClient>[0]): RestrictedClient { | ||
return restrictClient( | ||
limitClientConcurrency( | ||
createClient({...config, useCdn: false, requestTagPrefix: 'sanity.migration'}), | ||
), | ||
) | ||
} | ||
|
||
const ALLOWED_PROPERTIES = [ | ||
'fetch', | ||
'clone', | ||
'config', | ||
'withConfig', | ||
'getDocument', | ||
'getDocuments', | ||
'users', | ||
'projects', | ||
] as const | ||
|
||
type AllowedMethods = (typeof ALLOWED_PROPERTIES)[number] | ||
|
||
export type RestrictedClient = Pick<SanityClient, AllowedMethods> | ||
|
||
function restrictClient(client: SanityClient): RestrictedClient { | ||
return new Proxy(client, { | ||
get: (target, property) => { | ||
switch (property) { | ||
case 'clone': { | ||
return (...args: Parameters<SanityClient['clone']>) => { | ||
return restrictClient(target.clone(...args)) | ||
} | ||
} | ||
case 'config': { | ||
return (...args: Parameters<SanityClient['config']>) => { | ||
const result = target.config(...args) | ||
|
||
// if there is a config, it returns a client so we need to wrap again | ||
if (args[0]) return restrictClient(result) | ||
return result | ||
} | ||
} | ||
case 'withConfig': { | ||
return (...args: Parameters<SanityClient['withConfig']>) => { | ||
return restrictClient(target.withConfig(...args)) | ||
} | ||
} | ||
default: { | ||
if (ALLOWED_PROPERTIES.includes(property as any)) { | ||
return target[property as keyof SanityClient] | ||
} | ||
throw new Error( | ||
`Client method "${String( | ||
property, | ||
)}" can not be called during a migration. Only ${ALLOWED_PROPERTIES.join( | ||
', ', | ||
)} are allowed.`, | ||
) | ||
} | ||
} | ||
}, | ||
}) | ||
} |
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