@sanity/cli-core 0.0.2-alpha.0 → 0.0.2-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { type SanityClient } from '@sanity/client';
|
|
1
|
+
import { type ClientConfig, type SanityClient } from '@sanity/client';
|
|
2
2
|
/**
|
|
3
3
|
* @internal
|
|
4
4
|
*/
|
|
5
|
-
export interface GlobalCliClientOptions {
|
|
5
|
+
export interface GlobalCliClientOptions extends ClientConfig {
|
|
6
6
|
/**
|
|
7
7
|
* The API version to use for this client.
|
|
8
8
|
*/
|
|
@@ -20,14 +20,28 @@ export interface GlobalCliClientOptions {
|
|
|
20
20
|
* @param options - The options to use for the client.
|
|
21
21
|
* @returns Promise that resolves to a configured Sanity API client.
|
|
22
22
|
*/
|
|
23
|
-
export declare function getGlobalCliClient({
|
|
23
|
+
export declare function getGlobalCliClient({ requireUser, ...config }: GlobalCliClientOptions): Promise<SanityClient>;
|
|
24
24
|
/**
|
|
25
25
|
* @internal
|
|
26
26
|
*/
|
|
27
|
-
export interface ProjectCliClientOptions {
|
|
27
|
+
export interface ProjectCliClientOptions extends ClientConfig {
|
|
28
|
+
/**
|
|
29
|
+
* The API version to use for this client.
|
|
30
|
+
*/
|
|
28
31
|
apiVersion: string;
|
|
32
|
+
/**
|
|
33
|
+
* The project ID to use for this client.
|
|
34
|
+
*/
|
|
29
35
|
projectId: string;
|
|
36
|
+
/**
|
|
37
|
+
* The dataset to use for this client.
|
|
38
|
+
*/
|
|
30
39
|
dataset?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Whether to require a user to be authenticated to use this client.
|
|
42
|
+
* Default: `false`.
|
|
43
|
+
* Throws an error if `true` and user is not authenticated.
|
|
44
|
+
*/
|
|
31
45
|
requireUser?: boolean;
|
|
32
46
|
}
|
|
33
47
|
/**
|
|
@@ -36,4 +50,4 @@ export interface ProjectCliClientOptions {
|
|
|
36
50
|
* @param options - The options to use for the client.
|
|
37
51
|
* @returns Promise that resolves to a configured Sanity API client.
|
|
38
52
|
*/
|
|
39
|
-
export declare function getProjectCliClient({
|
|
53
|
+
export declare function getProjectCliClient({ requireUser, ...config }: ProjectCliClientOptions): Promise<SanityClient>;
|
|
@@ -12,7 +12,7 @@ const CLI_REQUEST_TAG_PREFIX = 'sanity.cli';
|
|
|
12
12
|
*
|
|
13
13
|
* @param options - The options to use for the client.
|
|
14
14
|
* @returns Promise that resolves to a configured Sanity API client.
|
|
15
|
-
*/ export async function getGlobalCliClient({
|
|
15
|
+
*/ export async function getGlobalCliClient({ requireUser, ...config }) {
|
|
16
16
|
const requester = defaultRequester.clone();
|
|
17
17
|
requester.use(authErrors());
|
|
18
18
|
const sanityEnv = process.env.SANITY_INTERNAL_ENV || 'production';
|
|
@@ -25,12 +25,12 @@ const CLI_REQUEST_TAG_PREFIX = 'sanity.cli';
|
|
|
25
25
|
...apiHost ? {
|
|
26
26
|
apiHost
|
|
27
27
|
} : {},
|
|
28
|
-
apiVersion,
|
|
29
28
|
requester,
|
|
30
29
|
requestTagPrefix: CLI_REQUEST_TAG_PREFIX,
|
|
31
30
|
token,
|
|
32
31
|
useCdn: false,
|
|
33
|
-
useProjectHostname: false
|
|
32
|
+
useProjectHostname: false,
|
|
33
|
+
...config
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
@@ -38,7 +38,7 @@ const CLI_REQUEST_TAG_PREFIX = 'sanity.cli';
|
|
|
38
38
|
*
|
|
39
39
|
* @param options - The options to use for the client.
|
|
40
40
|
* @returns Promise that resolves to a configured Sanity API client.
|
|
41
|
-
*/ export async function getProjectCliClient({
|
|
41
|
+
*/ export async function getProjectCliClient({ requireUser, ...config }) {
|
|
42
42
|
const requester = defaultRequester.clone();
|
|
43
43
|
requester.use(authErrors());
|
|
44
44
|
const sanityEnv = process.env.SANITY_INTERNAL_ENV || 'production';
|
|
@@ -51,14 +51,12 @@ const CLI_REQUEST_TAG_PREFIX = 'sanity.cli';
|
|
|
51
51
|
...apiHost ? {
|
|
52
52
|
apiHost
|
|
53
53
|
} : {},
|
|
54
|
-
apiVersion,
|
|
55
|
-
dataset,
|
|
56
|
-
projectId,
|
|
57
54
|
requester,
|
|
58
55
|
requestTagPrefix: CLI_REQUEST_TAG_PREFIX,
|
|
59
56
|
token,
|
|
60
57
|
useCdn: false,
|
|
61
|
-
useProjectHostname: true
|
|
58
|
+
useProjectHostname: true,
|
|
59
|
+
...config
|
|
62
60
|
});
|
|
63
61
|
}
|
|
64
62
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/services/apiClient.ts"],"sourcesContent":["import {ux} from '@oclif/core'\nimport {\n type ClientError,\n createClient,\n requester as defaultRequester,\n type SanityClient,\n type ServerError,\n} from '@sanity/client'\n\nimport {generateHelpUrl} from '../util/generateHelpUrl.js'\nimport {isHttpError} from '../util/isHttpError.js'\nimport {getCliToken} from './getCliToken.js'\n\nconst apiHosts: Record<string, string | undefined> = {\n staging: 'https://api.sanity.work',\n}\n\nconst CLI_REQUEST_TAG_PREFIX = 'sanity.cli'\n\n/**\n * @internal\n */\nexport interface GlobalCliClientOptions {\n /**\n * The API version to use for this client.\n */\n apiVersion: string\n\n /**\n * Whether to require a user to be authenticated to use this client.\n * Default: `false`.\n * Throws an error if `true` and user is not authenticated.\n */\n requireUser?: boolean\n}\n\n/**\n * Create a \"global\" (unscoped) Sanity API client.\n *\n * @param options - The options to use for the client.\n * @returns Promise that resolves to a configured Sanity API client.\n */\nexport async function getGlobalCliClient({\n
|
|
1
|
+
{"version":3,"sources":["../../src/services/apiClient.ts"],"sourcesContent":["import {ux} from '@oclif/core'\nimport {\n type ClientConfig,\n type ClientError,\n createClient,\n requester as defaultRequester,\n type SanityClient,\n type ServerError,\n} from '@sanity/client'\n\nimport {generateHelpUrl} from '../util/generateHelpUrl.js'\nimport {isHttpError} from '../util/isHttpError.js'\nimport {getCliToken} from './getCliToken.js'\n\nconst apiHosts: Record<string, string | undefined> = {\n staging: 'https://api.sanity.work',\n}\n\nconst CLI_REQUEST_TAG_PREFIX = 'sanity.cli'\n\n/**\n * @internal\n */\nexport interface GlobalCliClientOptions extends ClientConfig {\n /**\n * The API version to use for this client.\n */\n apiVersion: string\n\n /**\n * Whether to require a user to be authenticated to use this client.\n * Default: `false`.\n * Throws an error if `true` and user is not authenticated.\n */\n requireUser?: boolean\n}\n\n/**\n * Create a \"global\" (unscoped) Sanity API client.\n *\n * @param options - The options to use for the client.\n * @returns Promise that resolves to a configured Sanity API client.\n */\nexport async function getGlobalCliClient({\n requireUser,\n ...config\n}: GlobalCliClientOptions): Promise<SanityClient> {\n const requester = defaultRequester.clone()\n requester.use(authErrors())\n\n const sanityEnv = process.env.SANITY_INTERNAL_ENV || 'production'\n\n const token = await getCliToken()\n const apiHost = apiHosts[sanityEnv]\n\n if (requireUser && !token) {\n throw new Error('You must login first - run \"sanity login\"')\n }\n\n return createClient({\n ...(apiHost ? {apiHost} : {}),\n requester,\n requestTagPrefix: CLI_REQUEST_TAG_PREFIX,\n token,\n useCdn: false,\n useProjectHostname: false,\n ...config,\n })\n}\n\n/**\n * @internal\n */\nexport interface ProjectCliClientOptions extends ClientConfig {\n /**\n * The API version to use for this client.\n */\n apiVersion: string\n\n /**\n * The project ID to use for this client.\n */\n projectId: string\n\n /**\n * The dataset to use for this client.\n */\n dataset?: string\n\n /**\n * Whether to require a user to be authenticated to use this client.\n * Default: `false`.\n * Throws an error if `true` and user is not authenticated.\n */\n requireUser?: boolean\n}\n\n/**\n * Create a \"global\" (unscoped) Sanity API client.\n *\n * @param options - The options to use for the client.\n * @returns Promise that resolves to a configured Sanity API client.\n */\nexport async function getProjectCliClient({\n requireUser,\n ...config\n}: ProjectCliClientOptions): Promise<SanityClient> {\n const requester = defaultRequester.clone()\n requester.use(authErrors())\n\n const sanityEnv = process.env.SANITY_INTERNAL_ENV || 'production'\n\n const token = await getCliToken()\n const apiHost = apiHosts[sanityEnv]\n\n if (requireUser && !token) {\n throw new Error('You must login first - run \"sanity login\"')\n }\n\n return createClient({\n ...(apiHost ? {apiHost} : {}),\n requester,\n requestTagPrefix: CLI_REQUEST_TAG_PREFIX,\n token,\n useCdn: false,\n useProjectHostname: true,\n ...config,\n })\n}\n\n/**\n * `get-it` middleware that checks for 401 authentication errors and extends the error with more\n * helpful guidance on what to do next.\n *\n * @returns get-it middleware with `onError` handler\n * @internal\n */\nfunction authErrors() {\n return {\n onError: (err: Error | null) => {\n if (!err || !isReqResError(err)) {\n return err\n }\n\n const statusCode = isHttpError(err) && err.response.body.statusCode\n if (statusCode === 401) {\n err.message = `${err.message}. You may need to login again with ${ux.colorize('cyan', 'sanity login')}.\\nFor more information, see ${generateHelpUrl('cli-errors')}.`\n }\n\n return err\n },\n }\n}\n\nfunction isReqResError(err: Error): err is ClientError | ServerError {\n return Object.prototype.hasOwnProperty.call(err, 'response')\n}\n"],"names":["ux","createClient","requester","defaultRequester","generateHelpUrl","isHttpError","getCliToken","apiHosts","staging","CLI_REQUEST_TAG_PREFIX","getGlobalCliClient","requireUser","config","clone","use","authErrors","sanityEnv","process","env","SANITY_INTERNAL_ENV","token","apiHost","Error","requestTagPrefix","useCdn","useProjectHostname","getProjectCliClient","onError","err","isReqResError","statusCode","response","body","message","colorize","Object","prototype","hasOwnProperty","call"],"mappings":"AAAA,SAAQA,EAAE,QAAO,cAAa;AAC9B,SAGEC,YAAY,EACZC,aAAaC,gBAAgB,QAGxB,iBAAgB;AAEvB,SAAQC,eAAe,QAAO,6BAA4B;AAC1D,SAAQC,WAAW,QAAO,yBAAwB;AAClD,SAAQC,WAAW,QAAO,mBAAkB;AAE5C,MAAMC,WAA+C;IACnDC,SAAS;AACX;AAEA,MAAMC,yBAAyB;AAmB/B;;;;;CAKC,GACD,OAAO,eAAeC,mBAAmB,EACvCC,WAAW,EACX,GAAGC,QACoB;IACvB,MAAMV,YAAYC,iBAAiBU,KAAK;IACxCX,UAAUY,GAAG,CAACC;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