import{r as a,p as i,q as r,w as n,z as s,t as h,R as e}from"./vendor.3d5b4206.js";const d={class:"markdown-body"},p=s("blockquote",null,[s("p",null,[e("The JS SDK provides an intuitive interface for the Directus API from within a JavaScript-powered project (browsers and Node.js). The default implementation uses "),s("a",{href:"https://npmjs.com/axios",target:"_blank",rel:"noopener noreferrer"},"Axios"),e(" for transport and "),s("code",null,"localStorage"),e(" for storing state.")])],-1),u=s("p",null,[s("div",{class:"table-of-contents"},[s("ul",null,[s("li",null,[s("a",{href:"#installation"},"Installation")]),s("li",null,[s("a",{href:"#usage"},"Usage")]),s("li",null,[s("a",{href:"#typescript"},"TypeScript")]),s("li",null,[s("a",{href:"#reference"},"Reference")]),s("li",null,[s("a",{href:"#auth"},"Auth")]),s("li",null,[s("a",{href:"#transport"},"Transport")]),s("li",null,[s("a",{href:"#storage"},"Storage")]),s("li",null,[s("a",{href:"#items"},"Items")]),s("li",null,[s("a",{href:"#activity"},"Activity")]),s("li",null,[s("a",{href:"#comments"},"Comments")]),s("li",null,[s("a",{href:"#collections"},"Collections")]),s("li",null,[s("a",{href:"#fields"},"Fields")]),s("li",null,[s("a",{href:"#files"},"Files")]),s("li",null,[s("a",{href:"#folders"},"Folders")]),s("li",null,[s("a",{href:"#permissions"},"Permissions")]),s("li",null,[s("a",{href:"#presets"},"Presets")]),s("li",null,[s("a",{href:"#relations"},"Relations")]),s("li",null,[s("a",{href:"#revisions"},"Revisions")]),s("li",null,[s("a",{href:"#roles"},"Roles")]),s("li",null,[s("a",{href:"#settings"},"Settings")]),s("li",null,[s("a",{href:"#server"},"Server")]),s("li",null,[s("a",{href:"#users"},"Users")]),s("li",null,[s("a",{href:"#utils"},"Utils")])])])],-1),j=s("h2",{id:"installation"},[e("Installation "),s("a",{class:"header-anchor",href:"#installation"},"#")],-1),m=s("pre",null,[s("code",{class:"language-bash"},`npm install @directus/sdk
`)],-1),_=s("h2",{id:"usage"},[e("Usage "),s("a",{class:"header-anchor",href:"#usage"},"#")],-1),g=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"import"),e(" { Directus } "),s("span",{class:"hljs-keyword"},"from"),e(),s("span",{class:"hljs-string"},"'@directus/sdk'"),e(`;

`),s("span",{class:"hljs-keyword"},"const"),e(" directus = "),s("span",{class:"hljs-keyword"},"new"),e(" Directus("),s("span",{class:"hljs-string"},"'http://directus.example.com'"),e(`);

`),s("span",{class:"hljs-keyword"},"async"),e(),s("span",{class:"hljs-function"},[s("span",{class:"hljs-keyword"},"function"),e(),s("span",{class:"hljs-title"},"start"),e("("),s("span",{class:"hljs-params"}),e(") ")]),e(`{
	`),s("span",{class:"hljs-comment"},"// We don't need to authenticate if data is public"),e(`
	`),s("span",{class:"hljs-keyword"},"const"),e(" publicData = "),s("span",{class:"hljs-keyword"},"await"),e(" directus.items("),s("span",{class:"hljs-string"},"'public'"),e(").readMany({ "),s("span",{class:"hljs-attr"},"meta"),e(": "),s("span",{class:"hljs-string"},"'total_count'"),e(` });

	`),s("span",{class:"hljs-built_in"},"console"),e(`.log({
		`),s("span",{class:"hljs-attr"},"items"),e(`: publicData.data,
		`),s("span",{class:"hljs-attr"},"total"),e(`: publicData.meta.total_count,
	});

	`),s("span",{class:"hljs-comment"},"// But, we need to authenticate if data is private"),e(`
	`),s("span",{class:"hljs-keyword"},"let"),e(" authenticated = "),s("span",{class:"hljs-literal"},"false"),e(`;

	`),s("span",{class:"hljs-comment"},"// Try to authenticate with token if exists"),e(`
	`),s("span",{class:"hljs-keyword"},"await"),e(` directus.auth
		.refresh()
		.then(`),s("span",{class:"hljs-function"},"() =>"),e(` {
			authenticated = `),s("span",{class:"hljs-literal"},"true"),e(`;
		})
		.catch(`),s("span",{class:"hljs-function"},"() =>"),e(` {});

	`),s("span",{class:"hljs-comment"},"// Let's login in case we don't have token or it is invalid / expired"),e(`
	`),s("span",{class:"hljs-keyword"},"while"),e(` (!authenticated) {
		`),s("span",{class:"hljs-keyword"},"const"),e(" email = "),s("span",{class:"hljs-built_in"},"window"),e(".prompt("),s("span",{class:"hljs-string"},"'Email:'"),e(`);
		`),s("span",{class:"hljs-keyword"},"const"),e(" password = "),s("span",{class:"hljs-built_in"},"window"),e(".prompt("),s("span",{class:"hljs-string"},"'Password:'"),e(`);

		`),s("span",{class:"hljs-keyword"},"await"),e(` directus.auth
			.login({ email, password })
			.then(`),s("span",{class:"hljs-function"},"() =>"),e(` {
				authenticated = `),s("span",{class:"hljs-literal"},"true"),e(`;
			})
			.catch(`),s("span",{class:"hljs-function"},"() =>"),e(` {
				`),s("span",{class:"hljs-built_in"},"window"),e(".alert("),s("span",{class:"hljs-string"},"'Invalid credentials'"),e(`);
			});
	}

	`),s("span",{class:"hljs-comment"},"// After authentication, we can fetch the private data in case the user has access to it"),e(`
	`),s("span",{class:"hljs-keyword"},"const"),e(" privateData = "),s("span",{class:"hljs-keyword"},"await"),e(" directus.items("),s("span",{class:"hljs-string"},"'privateData'"),e(").readMany({ "),s("span",{class:"hljs-attr"},"meta"),e(": "),s("span",{class:"hljs-string"},"'total_count'"),e(` });

	`),s("span",{class:"hljs-built_in"},"console"),e(`.log({
		`),s("span",{class:"hljs-attr"},"items"),e(`: privateData.data,
		`),s("span",{class:"hljs-attr"},"total"),e(`: privateData.meta.total_count,
	});
}

start();
`)])],-1),f=s("h2",{id:"typescript"},[e("TypeScript "),s("a",{class:"header-anchor",href:"#typescript"},"#")],-1),y=s("p",null,"Version >= 4.1",-1),w=s("p",null,"Although it\u2019s not required, it is recommended to use Typescript to have an easy development experience. This allows more detailed IDE suggestions for return types, sorting, filtering, etc.",-1),k=s("p",null,"To feed the SDK with your current schema, you need to pass it on the constructor.",-1),b=s("pre",null,[s("code",{class:"language-ts"},[s("span",{class:"hljs-keyword"},"type"),e(` BlogPost = {
	`),s("span",{class:"hljs-attr"},"id"),e(`: ID;
	title: `),s("span",{class:"hljs-built_in"},"string"),e(`;
};

`),s("span",{class:"hljs-keyword"},"type"),e(` BlogSettings = {
	`),s("span",{class:"hljs-attr"},"display_promotions"),e(": "),s("span",{class:"hljs-built_in"},"boolean"),e(`;
};

`),s("span",{class:"hljs-keyword"},"type"),e(` MyCollections = {
	`),s("span",{class:"hljs-attr"},"posts"),e(`: BlogPost;
	settings: BlogSettings;
};

`),s("span",{class:"hljs-comment"},"// This is how you feed custom type information to Directus."),e(`
`),s("span",{class:"hljs-keyword"},"const"),e(" directus = "),s("span",{class:"hljs-keyword"},"new"),e(" Directus<MyCollections>("),s("span",{class:"hljs-string"},"'http://url'"),e(`);

`),s("span",{class:"hljs-comment"},"// ..."),e(`

`),s("span",{class:"hljs-keyword"},"const"),e(" post = "),s("span",{class:"hljs-keyword"},"await"),e(" directus.items("),s("span",{class:"hljs-string"},"'posts'"),e(").readOne("),s("span",{class:"hljs-number"},"1"),e(`);
`),s("span",{class:"hljs-comment"},"// typeof(post) is a partial BlogPost object"),e(`

`),s("span",{class:"hljs-keyword"},"const"),e(" settings = "),s("span",{class:"hljs-keyword"},"await"),e(" posts.singleton("),s("span",{class:"hljs-string"},"'settings'"),e(`).read();
`),s("span",{class:"hljs-comment"},"// typeof(settings) is a partial BlogSettings object"),e(`
`)])],-1),v=s("p",null,"You can also extend the Directus system type information by providing type information for system collections as well.",-1),S=s("pre",null,[s("code",{class:"language-ts"},[s("span",{class:"hljs-keyword"},"import"),e(" { Directus } "),s("span",{class:"hljs-keyword"},"from"),e(),s("span",{class:"hljs-string"},"'@directus/sdk'"),e(`;

`),s("span",{class:"hljs-comment"},"// Custom fields added to Directus user collection."),e(`
`),s("span",{class:"hljs-keyword"},"type"),e(` UserType = {
	`),s("span",{class:"hljs-attr"},"level"),e(": "),s("span",{class:"hljs-built_in"},"number"),e(`;
	experience: `),s("span",{class:"hljs-built_in"},"number"),e(`;
};

`),s("span",{class:"hljs-keyword"},"type"),e(` CustomTypes = {
	`),s("span",{class:"hljs-comment"},`/*
	This type will be merged with Directus user type.
	It's important that the naming matches a directus
	collection name exactly. Typos won't get caught here
	since SDK will assume it's a custom user collection.
	*/`),e(`
	`),s("span",{class:"hljs-attr"},"directus_users"),e(`: UserType;
};

`),s("span",{class:"hljs-keyword"},"const"),e(" directus = "),s("span",{class:"hljs-keyword"},"new"),e(" Directus<CustomTypes>("),s("span",{class:"hljs-string"},"'https://api.example.com'"),e(`);

`),s("span",{class:"hljs-keyword"},"await"),e(` directus.auth.login({
	`),s("span",{class:"hljs-attr"},"email"),e(": "),s("span",{class:"hljs-string"},"'admin@example.com'"),e(`,
	`),s("span",{class:"hljs-attr"},"password"),e(": "),s("span",{class:"hljs-string"},"'password'"),e(`,
});

`),s("span",{class:"hljs-keyword"},"const"),e(" me = "),s("span",{class:"hljs-keyword"},"await"),e(` directus.users.me.read();
`),s("span",{class:"hljs-comment"},"// typeof me = partial DirectusUser & UserType;"),e(`

`),s("span",{class:"hljs-comment"},"// OK"),e(`
me.level = `),s("span",{class:"hljs-number"},"42"),e(`;

`),s("span",{class:"hljs-comment"},'// Error TS2322: Type "string" is not assignable to type "number".'),e(`
me.experience = `),s("span",{class:"hljs-string"},"'high'"),e(`;
`)])],-1),D=s("h2",{id:"reference"},[e("Reference "),s("a",{class:"header-anchor",href:"#reference"},"#")],-1),T=s("h3",{id:"constructor"},[e("Constructor "),s("a",{class:"header-anchor",href:"#constructor"},"#")],-1),x=s("p",null,[e("This is the starting point to use the SDK. You need to create an instance and invoke methods from it. In most cases a single instance is sufficient, but in case you need more, you need to define "),s("a",{href:"#options.storage.prefix"},[s("code",null,"options.storage.prefix")]),e(".")],-1),I=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"import"),e(" { Directus } "),s("span",{class:"hljs-keyword"},"from"),e(),s("span",{class:"hljs-string"},"'@directus/sdk'"),e(`;

`),s("span",{class:"hljs-keyword"},"const"),e(" directus = "),s("span",{class:"hljs-keyword"},"new"),e(` Directus(url, init);
`)])],-1),A=s("ul",null,[s("li",null,[s("p",null,[s("code",null,"url"),e(" [required] "),s("em",null,"String"),e(" - A string pointing to your Directus instance. E.g. "),s("code",null,"https://admin.directus.io")]),s("p",null,[s("a",{name:"options"})])]),s("li",null,[s("p",null,[s("code",null,"init"),e(" [optional] "),s("em",null,"Object"),e(" - Define settings that you want to customize .The possible options are:")]),s("p",null,[s("a",{name:"options.auth"})]),s("ul",null,[s("li",null,[s("p",null,[s("code",null,"auth"),e(" [optional] "),s("em",null,"Object"),e(" - Defines settings you want to customize regarding "),s("a",{href:"#auth"},"authentication"),e(". The possible options are:")]),s("ul",null,[s("li",null,[s("p",null,[s("code",null,"mode"),e(" [optional] "),s("em",null,"String"),e(" - Defines the mode you want to use for authentication. It can be "),s("code",null,"'cookie'"),e(" for cookies or "),s("code",null,"'json'"),e(" for JWT. Defaults to "),s("code",null,"'cookie'"),e(" on browsers and "),s("code",null,"'json'"),e(" otherwise. We recommend using cookies when possible to prevent any kind of attacks, mostly XSS.")]),s("p",null,[s("a",{name:"options.auth.autoRefresh"})])]),s("li",null,[s("p",null,[s("code",null,"autoRefresh"),e(" [optional] "),s("em",null,"Boolean"),e(" - Tells SDK if it should handle refresh tokens automatically. Defaults to "),s("code",null,"true"),e(".")])]),s("li",null,[s("p",null,[s("code",null,"msRefreshBeforeExpires"),e(" [optional] "),s("em",null,"Number"),e(" - When "),s("code",null,"autoRefresh"),e(" is enabled, this tells how many milliseconds before the refresh token expires and needs to be refreshed. Defaults to "),s("code",null,"30000"),e(".")])]),s("li",null,[s("p",null,[s("code",null,"staticToken"),e(" [optional] "),s("em",null,"String"),e(" - Defines the static token to use. It is not compatible with the options above since it does not refresh. Defaults to "),s("code",null,"''"),e(" (no static token).")])])])])]),s("p",null,[s("a",{name:"options.storage"})]),s("ul",null,[s("li",null,[s("p",null,[s("code",null,"storage"),e(" [optional] "),s("em",null,"Object"),e(" - Defines settings you want to customize regarding "),s("a",{href:"#storage"},"storage"),e(".")]),s("p",null,[s("a",{name:"options.storage.prefix"})]),s("ul",null,[s("li",null,[s("code",null,"prefix"),e(" [optional] "),s("em",null,"String"),e(" - Defines the tokens prefix tokens that are saved. This should be fulfilled with different values when using multiple instances of SDK. Defaults to "),s("code",null,"''"),e(" (no prefix).")]),s("li",null,[s("code",null,"mode"),e(" [optional] "),s("em",null,"String"),e(" - Defines the storage location to be used to save tokens. Allowed values are "),s("code",null,"LocalStorage"),e(" and "),s("code",null,"MemoryStorage"),e(". Defaults to "),s("code",null,"LocalStorage"),e(" on browsers and "),s("code",null,"MemoryStorage"),e(" on Node.js. The mode "),s("code",null,"LocalStorage"),e(" is not compatible with Node.js.")])])])]),s("p",null,[s("a",{name:"options.transport"})]),s("ul",null,[s("li",null,[s("code",null,"transport"),e(" [optional] "),s("em",null,"Object"),e(" - Defines settings you want to customize regarding "),s("a",{href:"#transport"},"transport"),e(". "),s("ul",null,[s("li",null,[s("code",null,"params"),e(" [optional] "),s("em",null,"Object"),e(" - Defines an object with keys and values to be passed as additional query string.")]),s("li",null,[s("code",null,"headers"),e(" [optional] "),s("em",null,"Object"),e(" - Defines an object with keys and values to be passed as additional headers.")]),s("li",null,[s("code",null,"onUploadProgress"),e(" [optional] "),s("em",null,[e("(event: "),s("a",{href:"https://developer.mozilla.org/en-US/docs/Web/API/ProgressEvent",target:"_blank",rel:"noopener noreferrer"},"ProgressEvent"),e(" => void)")]),e(" - Defines a callback function to indicate the upload progress.")])])])])])],-1),R=s("h2",{id:"auth"},[e("Auth "),s("a",{class:"header-anchor",href:"#auth"},"#")],-1),B=s("p",null,"Defines how authentication is handled by the SDK.",-1),M=s("h3",{id:"custom-implementation"},[e("Custom Implementation "),s("a",{class:"header-anchor",href:"#custom-implementation"},"#")],-1),q=s("p",null,[e("It is possible to provide a custom implementation by extending "),s("code",null,"IAuth"),e(". While this could be useful for advanced usage, most use-cases do not need it.")],-1),C=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"import"),e(" { IAuth, Directus } "),s("span",{class:"hljs-keyword"},"from"),e(),s("span",{class:"hljs-string"},"'@directus/sdk'"),e(`;

`),s("span",{class:"hljs-class"},[s("span",{class:"hljs-keyword"},"class"),e(),s("span",{class:"hljs-title"},"MyAuth"),e(),s("span",{class:"hljs-keyword"},"extends"),e(),s("span",{class:"hljs-title"},"IAuth"),e()]),e(`{
	`),s("span",{class:"hljs-keyword"},"async"),e(),s("span",{class:"hljs-function"},[s("span",{class:"hljs-title"},"login"),e("("),s("span",{class:"hljs-params"}),e(")")]),e(` {
		`),s("span",{class:"hljs-keyword"},"return"),e(" { "),s("span",{class:"hljs-attr"},"access_token"),e(": "),s("span",{class:"hljs-string"},"''"),e(", "),s("span",{class:"hljs-attr"},"expires"),e(": "),s("span",{class:"hljs-number"},"0"),e(` };
	}
	`),s("span",{class:"hljs-keyword"},"async"),e(),s("span",{class:"hljs-function"},[s("span",{class:"hljs-title"},"logout"),e("("),s("span",{class:"hljs-params"}),e(")")]),e(` {}
	`),s("span",{class:"hljs-keyword"},"async"),e(),s("span",{class:"hljs-function"},[s("span",{class:"hljs-title"},"refresh"),e("("),s("span",{class:"hljs-params"}),e(")")]),e(` {
		`),s("span",{class:"hljs-keyword"},"return"),e(" { "),s("span",{class:"hljs-attr"},"access_token"),e(": "),s("span",{class:"hljs-string"},"''"),e(", "),s("span",{class:"hljs-attr"},"expires"),e(": "),s("span",{class:"hljs-number"},"0"),e(` };
	}
	`),s("span",{class:"hljs-keyword"},"async"),e(),s("span",{class:"hljs-function"},[s("span",{class:"hljs-title"},"static"),e("("),s("span",{class:"hljs-params"}),e(")")]),e(` {
		`),s("span",{class:"hljs-keyword"},"return"),e(),s("span",{class:"hljs-literal"},"true"),e(`;
	}
}

`),s("span",{class:"hljs-keyword"},"const"),e(" directus = "),s("span",{class:"hljs-keyword"},"new"),e(" Directus("),s("span",{class:"hljs-string"},"'http://directus.example.com'"),e(", { "),s("span",{class:"hljs-attr"},"auth"),e(": "),s("span",{class:"hljs-keyword"},"new"),e(` MyAuth() });
`)])],-1),P=s("h3",{id:"directus-implementation"},[e("Directus Implementation "),s("a",{class:"header-anchor",href:"#directus-implementation"},"#")],-1),U=s("p",null,[e("By default, Directus creates an instance of "),s("code",null,"Auth"),e(" which handles refresh tokens automatically. Check "),s("a",{href:"#options.auth"},[s("code",null,"options.auth")]),e(" to see the available settings.")],-1),O=s("h3",{id:"get-current-token"},[e("Get current token "),s("a",{class:"header-anchor",href:"#get-current-token"},"#")],-1),K=s("pre",null,[s("code",{class:"language-ts"},[s("span",{class:"hljs-keyword"},"const"),e(` token = directus.auth.token;
`)])],-1),E=s("h3",{id:"login"},[e("Login "),s("a",{class:"header-anchor",href:"#login"},"#")],-1),F=s("h4",{id:"with-credentials"},[e("With credentials "),s("a",{class:"header-anchor",href:"#with-credentials"},"#")],-1),N=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(` directus.auth.login({
	`),s("span",{class:"hljs-attr"},"email"),e(": "),s("span",{class:"hljs-string"},"'admin@example.com'"),e(`,
	`),s("span",{class:"hljs-attr"},"password"),e(": "),s("span",{class:"hljs-string"},"'d1r3ctu5'"),e(`,
});
`)])],-1),W=s("h4",{id:"with-static-tokens"},[e("With static tokens "),s("a",{class:"header-anchor",href:"#with-static-tokens"},"#")],-1),L=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" directus.auth.static("),s("span",{class:"hljs-string"},"'static_token'"),e(`);
`)])],-1),z=s("h3",{id:"refresh-auth-token"},[e("Refresh Auth Token "),s("a",{class:"header-anchor",href:"#refresh-auth-token"},"#")],-1),V=s("p",null,[e("By default, Directus will handle token refreshes. Although, you can handle this behaviour manually by setting "),s("a",{href:"#options.auth.autoRefresh"},[s("code",null,"autoRefresh")]),e(" to "),s("code",null,"false"),e(".")],-1),G=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(` directus.auth.refresh();
`)])],-1),J={class:"tip hint"},Y=s("div",{class:"hint-title"},"Developing Locally",-1),H=e("If you\u2019re developing locally, you might not be able to refresh your auth token automatically in all browsers. This is because the default auth configuration requires secure cookies to be set, and not all browsers allow this for localhost. You can use a browser which does support this such as Firefox, or "),$=e("disable secure cookies"),Q=e("."),X=s("h3",{id:"logout"},[e("Logout "),s("a",{class:"header-anchor",href:"#logout"},"#")],-1),Z=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(` directus.auth.logout();
`)])],-1),ss=s("h3",{id:"request-a-password-reset"},[e("Request a Password Reset "),s("a",{class:"header-anchor",href:"#request-a-password-reset"},"#")],-1),es=s("p",null,[e("By default, the address defined in "),s("code",null,"PUBLIC_URL"),e(" on "),s("code",null,".env"),e(" file is used for the link to the reset password page sent in the email:")],-1),ts=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" directus.auth.password.request("),s("span",{class:"hljs-string"},"'admin@example.com'"),e(`);
`)])],-1),as=s("p",null,"But a custom address can be passed as second argument:",-1),ns=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(` directus.auth.password.request(
	`),s("span",{class:"hljs-string"},"'admin@example.com'"),e(`,
	`),s("span",{class:"hljs-string"},"'https://myapp.com'"),e(),s("span",{class:"hljs-comment"},"// In this case, the link will be https://myapp.com?token=FEE0A..."),e(`
);
`)])],-1),ls=s("h3",{id:"reset-a-password"},[e("Reset a Password "),s("a",{class:"header-anchor",href:"#reset-a-password"},"#")],-1),os=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" directus.auth.password.reset("),s("span",{class:"hljs-string"},"'abc.def.ghi'"),e(", "),s("span",{class:"hljs-string"},"'n3w-p455w0rd'"),e(`);
`)])],-1),cs=s("p",null,[e("Note: The token passed in the first parameter is sent in an email to the user when using "),s("code",null,"request()")],-1),is=s("h2",{id:"transport"},[e("Transport "),s("a",{class:"header-anchor",href:"#transport"},"#")],-1),rs=s("p",null,"The transport object abstracts how you communicate with Directus. Transports can be customized to use different HTTP libraries for example.",-1),hs=s("h3",{id:"custom-implementation-2"},[e("Custom Implementation "),s("a",{class:"header-anchor",href:"#custom-implementation-2"},"#")],-1),ds=s("p",null,[e("It is possible to provide a custom implementation by extending "),s("code",null,"ITransport"),e(". While, this could be useful for advanced usage, it is not needed for most use-cases.")],-1),ps=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"import"),e(" { ITransport, Directus } "),s("span",{class:"hljs-keyword"},"from"),e(),s("span",{class:"hljs-string"},"'@directus/sdk'"),e(`;

`),s("span",{class:"hljs-class"},[s("span",{class:"hljs-keyword"},"class"),e(),s("span",{class:"hljs-title"},"MyTransport"),e(),s("span",{class:"hljs-keyword"},"extends"),e(),s("span",{class:"hljs-title"},"ITransport"),e()]),e(`{
	`),s("span",{class:"hljs-function"},[s("span",{class:"hljs-title"},"buildResponse"),e("("),s("span",{class:"hljs-params"}),e(")")]),e(` {
		`),s("span",{class:"hljs-keyword"},"return"),e(` {
			`),s("span",{class:"hljs-attr"},"raw"),e(": "),s("span",{class:"hljs-string"},"''"),e(`,
			`),s("span",{class:"hljs-attr"},"data"),e(`: {},
			`),s("span",{class:"hljs-attr"},"status"),e(": "),s("span",{class:"hljs-number"},"0"),e(`,
			`),s("span",{class:"hljs-attr"},"headers"),e(`: {},
		};
	}

	`),s("span",{class:"hljs-keyword"},"async"),e(),s("span",{class:"hljs-function"},[s("span",{class:"hljs-title"},"get"),e("("),s("span",{class:"hljs-params"},"path, options"),e(")")]),e(` {
		`),s("span",{class:"hljs-keyword"},"return"),e(),s("span",{class:"hljs-built_in"},"this"),e(`.buildResponse();
	}
	`),s("span",{class:"hljs-keyword"},"async"),e(),s("span",{class:"hljs-function"},[s("span",{class:"hljs-title"},"head"),e("("),s("span",{class:"hljs-params"},"path, options"),e(")")]),e(` {
		`),s("span",{class:"hljs-keyword"},"return"),e(),s("span",{class:"hljs-built_in"},"this"),e(`.buildResponse();
	}
	`),s("span",{class:"hljs-keyword"},"async"),e(),s("span",{class:"hljs-function"},[s("span",{class:"hljs-title"},"options"),e("("),s("span",{class:"hljs-params"},"path, options"),e(")")]),e(` {
		`),s("span",{class:"hljs-keyword"},"return"),e(),s("span",{class:"hljs-built_in"},"this"),e(`.buildResponse();
	}
	`),s("span",{class:"hljs-keyword"},"async"),e(),s("span",{class:"hljs-function"},[s("span",{class:"hljs-title"},"delete"),e("("),s("span",{class:"hljs-params"},"path, data, options"),e(")")]),e(` {
		`),s("span",{class:"hljs-keyword"},"return"),e(),s("span",{class:"hljs-built_in"},"this"),e(`.buildResponse();
	}
	`),s("span",{class:"hljs-keyword"},"async"),e(),s("span",{class:"hljs-function"},[s("span",{class:"hljs-title"},"post"),e("("),s("span",{class:"hljs-params"},"path, data, options"),e(")")]),e(` {
		`),s("span",{class:"hljs-keyword"},"return"),e(),s("span",{class:"hljs-built_in"},"this"),e(`.buildResponse();
	}
	`),s("span",{class:"hljs-keyword"},"async"),e(),s("span",{class:"hljs-function"},[s("span",{class:"hljs-title"},"put"),e("("),s("span",{class:"hljs-params"},"path, data, options"),e(")")]),e(` {
		`),s("span",{class:"hljs-keyword"},"return"),e(),s("span",{class:"hljs-built_in"},"this"),e(`.buildResponse();
	}
	`),s("span",{class:"hljs-keyword"},"async"),e(),s("span",{class:"hljs-function"},[s("span",{class:"hljs-title"},"patch"),e("("),s("span",{class:"hljs-params"},"path, data, options"),e(")")]),e(` {
		`),s("span",{class:"hljs-keyword"},"return"),e(),s("span",{class:"hljs-built_in"},"this"),e(`.buildResponse();
	}
}

`),s("span",{class:"hljs-keyword"},"const"),e(" directus = "),s("span",{class:"hljs-keyword"},"new"),e(" Directus("),s("span",{class:"hljs-string"},"'http://directus.example.com'"),e(", { "),s("span",{class:"hljs-attr"},"transport"),e(": "),s("span",{class:"hljs-keyword"},"new"),e(` MyTransport() });
`)])],-1),us=s("h3",{id:"directus-implementation-2"},[e("Directus Implementation "),s("a",{class:"header-anchor",href:"#directus-implementation-2"},"#")],-1),js=s("p",null,[e("By default, Directus creates an instance of "),s("code",null,"Transport"),e(" which handles requests automatically. Check "),s("a",{href:"#options.transport"},[s("code",null,"options.transport")]),e(" to see the available settings.")],-1),ms=s("p",null,[e("To make HTTP requests SDK uses "),s("code",null,"axios"),e(" so it is compatible in both browsers and Node.js. Also, it is possible to handle upload progress (a downside of "),s("code",null,"fetch"),e(").")],-1),_s=s("h2",{id:"storage"},[e("Storage "),s("a",{class:"header-anchor",href:"#storage"},"#")],-1),gs=s("p",null,"The storage is used to load and save token information.",-1),fs=s("h3",{id:"custom-implementation-3"},[e("Custom Implementation "),s("a",{class:"header-anchor",href:"#custom-implementation-3"},"#")],-1),ys=s("p",null,[e("It is possible to provide a custom implementation by extending "),s("code",null,"BaseStorage"),e(". While, this could be useful for advanced usage, it is not needed for most use-cases.")],-1),ws=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"import"),e(" { BaseStorage, Directus } "),s("span",{class:"hljs-keyword"},"from"),e(),s("span",{class:"hljs-string"},"'@directus/sdk'"),e(`;

`),s("span",{class:"hljs-class"},[s("span",{class:"hljs-keyword"},"class"),e(),s("span",{class:"hljs-title"},"SessionStorage"),e(),s("span",{class:"hljs-keyword"},"extends"),e(),s("span",{class:"hljs-title"},"BaseStorage"),e()]),e(`{
	`),s("span",{class:"hljs-function"},[s("span",{class:"hljs-title"},"get"),e("("),s("span",{class:"hljs-params"},"key"),e(")")]),e(` {
		`),s("span",{class:"hljs-keyword"},"return"),e(` sessionStorage.getItem(key);
	}
	`),s("span",{class:"hljs-function"},[s("span",{class:"hljs-title"},"set"),e("("),s("span",{class:"hljs-params"},"key, value"),e(")")]),e(` {
		`),s("span",{class:"hljs-keyword"},"return"),e(` sessionStorage.setItem(key, value);
	}
	`),s("span",{class:"hljs-function"},[s("span",{class:"hljs-title"},"delete"),e("("),s("span",{class:"hljs-params"},"key"),e(")")]),e(` {
		`),s("span",{class:"hljs-keyword"},"return"),e(` sessionStorage.removeItem(key);
	}
}

`),s("span",{class:"hljs-keyword"},"const"),e(" directus = "),s("span",{class:"hljs-keyword"},"new"),e(" Directus("),s("span",{class:"hljs-string"},"'http://directus.example.com'"),e(", { "),s("span",{class:"hljs-attr"},"storage"),e(": "),s("span",{class:"hljs-keyword"},"new"),e(` SessionStorage() });
`)])],-1),ks=s("h3",{id:"directus-implementation-3"},[e("Directus Implementation "),s("a",{class:"header-anchor",href:"#directus-implementation-3"},"#")],-1),bs=s("p",null,[e("By default, Directus creates an instance of "),s("code",null,"Storage"),e(" which handles store information automatically. Check "),s("a",{href:"#options.storage"},[s("code",null,"options.storage")]),e(" to see the available settings.")],-1),vs=s("p",null,[e("SDK uses "),s("code",null,"localStorage"),e(" on browsers and the memory itself on Node.js to save tokens. This behavior can be configured in "),s("a",{href:"#options.storage.mode"},[s("code",null,"options.storage.mode")]),e(". The "),s("code",null,"LocalStorage"),e(" is only available on browsers and the "),s("code",null,"MemoryStorage"),e(" is not persistent, i.e., once you leave the tab or quit the process, you will need to authenticate again.")],-1),Ss=s("p",null,[e("If you want to use multiple instances of the SDK you should set a different "),s("a",{href:"#options.storage.prefix"},[s("code",null,"prefix")]),e(" for each one.")],-1),Ds=s("p",null,"When using",-1),Ts=s("h2",{id:"items"},[e("Items "),s("a",{class:"header-anchor",href:"#items"},"#")],-1),xs=s("p",null,[e("You can get an instance of the item handler by providing the collection (and type, in the case of TypeScript) to the "),s("code",null,"items"),e(" function. The following examples will use the "),s("code",null,"Article"),e(" type.")],-1),Is=s("blockquote",null,[s("p",null,"JavaScript")],-1),As=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-comment"},"// import { Directus, ID } from '@directus/sdk';"),e(`
`),s("span",{class:"hljs-keyword"},"const"),e(" { Directus } = "),s("span",{class:"hljs-built_in"},"require"),e("("),s("span",{class:"hljs-string"},"'@directus/sdk'"),e(`);

`),s("span",{class:"hljs-keyword"},"const"),e(" directus = "),s("span",{class:"hljs-keyword"},"new"),e(" Directus("),s("span",{class:"hljs-string"},"'http://directus.example.com'"),e(`);

`),s("span",{class:"hljs-keyword"},"const"),e(" articles = directus.items("),s("span",{class:"hljs-string"},"'articles'"),e(`);
`)])],-1),Rs=s("blockquote",null,[s("p",null,"TypeScript")],-1),Bs=s("pre",null,[s("code",{class:"language-ts"},[s("span",{class:"hljs-keyword"},"import"),e(" { Directus, ID } "),s("span",{class:"hljs-keyword"},"from"),e(),s("span",{class:"hljs-string"},"'@directus/sdk'"),e(`;

`),s("span",{class:"hljs-comment"},"// Map your collection structure based on its fields."),e(`
`),s("span",{class:"hljs-keyword"},"type"),e(` Article = {
	`),s("span",{class:"hljs-attr"},"id"),e(`: ID;
	title: `),s("span",{class:"hljs-built_in"},"string"),e(`;
	body: `),s("span",{class:"hljs-built_in"},"string"),e(`;
	published: `),s("span",{class:"hljs-built_in"},"boolean"),e(`;
};

`),s("span",{class:"hljs-comment"},"// Map your collections to its respective types. The SDK will"),e(`
`),s("span",{class:"hljs-comment"},"// infer its types based on usage later."),e(`
`),s("span",{class:"hljs-keyword"},"type"),e(` MyBlog = {
	`),s("span",{class:"hljs-comment"},"// [collection_name]: typescript_type"),e(`
	`),s("span",{class:"hljs-attr"},"articles"),e(`: Article;

	`),s("span",{class:"hljs-comment"},"// You can also extend Directus collection. The naming has"),e(`
	`),s("span",{class:"hljs-comment"},"// to match a Directus system collection and it will be merged"),e(`
	`),s("span",{class:"hljs-comment"},"// into the system spec."),e(`
	directus_users: {
		`),s("span",{class:"hljs-attr"},"bio"),e(": "),s("span",{class:"hljs-built_in"},"string"),e(`;
	};
};

`),s("span",{class:"hljs-comment"},"// Let the SDK know about your collection types."),e(`
`),s("span",{class:"hljs-keyword"},"const"),e(" directus = "),s("span",{class:"hljs-keyword"},"new"),e(" Directus<MyBlog>("),s("span",{class:"hljs-string"},"'https://directus.myblog.com'"),e(`);

`),s("span",{class:"hljs-comment"},'// typeof(article) is a partial "Article"'),e(`
`),s("span",{class:"hljs-keyword"},"const"),e(" article = "),s("span",{class:"hljs-keyword"},"await"),e(" directus.items("),s("span",{class:"hljs-string"},"'articles'"),e(").readOne("),s("span",{class:"hljs-number"},"10"),e(`);

`),s("span",{class:"hljs-comment"},'// Error TS2322: "hello" is not assignable to type "boolean".'),e(`
`),s("span",{class:"hljs-comment"},"// post.published = 'hello';"),e(`
`)])],-1),Ms=s("h3",{id:"create-single-item"},[e("Create Single Item "),s("a",{class:"header-anchor",href:"#create-single-item"},"#")],-1),qs=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(` articles.createOne({
	`),s("span",{class:"hljs-attr"},"title"),e(": "),s("span",{class:"hljs-string"},"'My New Article'"),e(`,
});
`)])],-1),Cs=s("h3",{id:"create-multiple-items"},[e("Create Multiple Items "),s("a",{class:"header-anchor",href:"#create-multiple-items"},"#")],-1),Ps=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(` articles.createMany([
	{
		`),s("span",{class:"hljs-attr"},"title"),e(": "),s("span",{class:"hljs-string"},"'My First Article'"),e(`,
	},
	{
		`),s("span",{class:"hljs-attr"},"title"),e(": "),s("span",{class:"hljs-string"},"'My Second Article'"),e(`,
	},
]);
`)])],-1),Us=s("h3",{id:"read-all"},[e("Read All "),s("a",{class:"header-anchor",href:"#read-all"},"#")],-1),Os=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(` articles.readMany();
`)])],-1),Ks=s("h3",{id:"read-by-query"},[e("Read By Query "),s("a",{class:"header-anchor",href:"#read-by-query"},"#")],-1),Es=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(` articles.readByQuery({
	`),s("span",{class:"hljs-attr"},"search"),e(": "),s("span",{class:"hljs-string"},"'Directus'"),e(`,
	`),s("span",{class:"hljs-attr"},"filter"),e(`: {
		`),s("span",{class:"hljs-attr"},"date_published"),e(`: {
			`),s("span",{class:"hljs-attr"},"_gte"),e(": "),s("span",{class:"hljs-string"},"'$NOW'"),e(`,
		},
	},
});
`)])],-1),Fs=s("h3",{id:"read-by-primary-key(s)"},[e("Read By Primary Key(s) "),s("a",{class:"header-anchor",href:"#read-by-primary-key(s)"},"#")],-1),Ns=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" articles.readOne("),s("span",{class:"hljs-number"},"15"),e(`);
`)])],-1),Ws=s("p",null,"Supports optional query:",-1),Ls=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" articles.readOne("),s("span",{class:"hljs-number"},"15"),e(", { "),s("span",{class:"hljs-attr"},"fields"),e(": ["),s("span",{class:"hljs-string"},"'title'"),e(`] });
`)])],-1),zs=s("h3",{id:"update-multiple-items"},[e("Update Multiple Items "),s("a",{class:"header-anchor",href:"#update-multiple-items"},"#")],-1),Vs=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" articles.updateMany(["),s("span",{class:"hljs-number"},"15"),e(", "),s("span",{class:"hljs-number"},"42"),e(`], {
	`),s("span",{class:"hljs-attr"},"title"),e(": "),s("span",{class:"hljs-string"},"'Both articles now have the same title'"),e(`,
});
`)])],-1),Gs=s("p",null,"Supports optional query:",-1),Js=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(` articles.updateMany(
	[`),s("span",{class:"hljs-number"},"15"),e(", "),s("span",{class:"hljs-number"},"42"),e(`],
	{
		`),s("span",{class:"hljs-attr"},"title"),e(": "),s("span",{class:"hljs-string"},"'Both articles now have the same title'"),e(`,
	},
	{
		`),s("span",{class:"hljs-attr"},"fields"),e(": ["),s("span",{class:"hljs-string"},"'title'"),e(`],
	}
);
`)])],-1),Ys=s("h3",{id:"delete"},[e("Delete "),s("a",{class:"header-anchor",href:"#delete"},"#")],-1),Hs=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-comment"},"// One"),e(`
`),s("span",{class:"hljs-keyword"},"await"),e(" articles.deleteOne("),s("span",{class:"hljs-number"},"15"),e(`);

`),s("span",{class:"hljs-comment"},"// Multiple"),e(`
`),s("span",{class:"hljs-keyword"},"await"),e(" articles.deleteMany(["),s("span",{class:"hljs-number"},"15"),e(", "),s("span",{class:"hljs-number"},"42"),e(`]);
`)])],-1),$s=s("h2",{id:"activity"},[e("Activity "),s("a",{class:"header-anchor",href:"#activity"},"#")],-1),Qs=s("pre",null,[s("code",{class:"language-js"},`directus.activity;
`)],-1),Xs=s("p",null,[e("Same methods as "),s("code",null,'directus.items("directus_activity")'),e(".")],-1),Zs=s("h2",{id:"comments"},[e("Comments "),s("a",{class:"header-anchor",href:"#comments"},"#")],-1),se=s("pre",null,[s("code",{class:"language-js"},`directus.comments;
`)],-1),ee=s("p",null,[e("Same methods as "),s("code",null,'directus.items("directus_comments")'),e(".")],-1),te=s("h2",{id:"collections"},[e("Collections "),s("a",{class:"header-anchor",href:"#collections"},"#")],-1),ae=s("pre",null,[s("code",{class:"language-js"},`directus.collections;
`)],-1),ne=s("p",null,[e("Same methods as "),s("code",null,'directus.items("directus_collections")'),e(".")],-1),le=s("h2",{id:"fields"},[e("Fields "),s("a",{class:"header-anchor",href:"#fields"},"#")],-1),oe=s("pre",null,[s("code",{class:"language-js"},`directus.fields;
`)],-1),ce=s("p",null,[e("Same methods as "),s("code",null,'directus.items("directus_fields")'),e(".")],-1),ie=s("h2",{id:"files"},[e("Files "),s("a",{class:"header-anchor",href:"#files"},"#")],-1),re=s("pre",null,[s("code",{class:"language-js"},`directus.files;
`)],-1),he=s("p",null,[e("Same methods as "),s("code",null,'directus.items("directus_files")'),e(".")],-1),de=s("h3",{id:"uploading-a-file"},[e("Uploading a file "),s("a",{class:"header-anchor",href:"#uploading-a-file"},"#")],-1),pe=s("p",null,[e("To upload a file you will need to send a "),s("code",null,"multipart/form-data"),e(" as body. On browser side you do so:")],-1),ue=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-comment"},"/* index.js */"),e(`
`),s("span",{class:"hljs-keyword"},"import"),e(" { Directus } "),s("span",{class:"hljs-keyword"},"from"),e(),s("span",{class:"hljs-string"},"'https://unpkg.com/@directus/sdk@latest/dist/sdk.esm.min.js'"),e(`;

`),s("span",{class:"hljs-keyword"},"const"),e(" directus = "),s("span",{class:"hljs-keyword"},"new"),e(" Directus("),s("span",{class:"hljs-string"},"'http://localhost:8055'"),e(`, {
	`),s("span",{class:"hljs-attr"},"auth"),e(`: {
		`),s("span",{class:"hljs-attr"},"staticToken"),e(": "),s("span",{class:"hljs-string"},"'STATIC_TOKEN'"),e(", "),s("span",{class:"hljs-comment"},"// If you want to use a static token, otherwise check below how you can use email and password."),e(`
	},
});

`),s("span",{class:"hljs-comment"},"// await directus.auth.login({ email, password }) // If you want to use email and password. You should remove the staticToken above"),e(`

`),s("span",{class:"hljs-keyword"},"const"),e(" form = "),s("span",{class:"hljs-built_in"},"document"),e(".querySelector("),s("span",{class:"hljs-string"},"'#upload-file'"),e(`);

`),s("span",{class:"hljs-keyword"},"if"),e(" (form && form "),s("span",{class:"hljs-keyword"},"instanceof"),e(` HTMLFormElement) {
	form.addEventListener(`),s("span",{class:"hljs-string"},"'submit'"),e(", "),s("span",{class:"hljs-keyword"},"async"),e(` (event) => {
		event.preventDefault();

		`),s("span",{class:"hljs-keyword"},"const"),e(" form = "),s("span",{class:"hljs-keyword"},"new"),e(` FormData(event.target);
		`),s("span",{class:"hljs-keyword"},"await"),e(` directus.files.createOne(form);
	});
}
`)])],-1),je=s("pre",null,[s("code",{class:"language-html"},[s("span",{class:"hljs-comment"},"<!-- index.html -->"),e(`
`),s("span",{class:"hljs-tag"},[e("<"),s("span",{class:"hljs-name"},"head"),e(">")]),s("span",{class:"hljs-tag"},[e("</"),s("span",{class:"hljs-name"},"head"),e(">")]),e(`
`),s("span",{class:"hljs-tag"},[e("<"),s("span",{class:"hljs-name"},"body"),e(">")]),e(`
	`),s("span",{class:"hljs-tag"},[e("<"),s("span",{class:"hljs-name"},"form"),e(),s("span",{class:"hljs-attr"},"id"),e("="),s("span",{class:"hljs-string"},'"upload-file"'),e(">")]),e(`
		`),s("span",{class:"hljs-tag"},[e("<"),s("span",{class:"hljs-name"},"input"),e(),s("span",{class:"hljs-attr"},"type"),e("="),s("span",{class:"hljs-string"},'"text"'),e(),s("span",{class:"hljs-attr"},"name"),e("="),s("span",{class:"hljs-string"},'"title"'),e(" />")]),e(`
		`),s("span",{class:"hljs-tag"},[e("<"),s("span",{class:"hljs-name"},"input"),e(),s("span",{class:"hljs-attr"},"type"),e("="),s("span",{class:"hljs-string"},'"file"'),e(),s("span",{class:"hljs-attr"},"name"),e("="),s("span",{class:"hljs-string"},'"file"'),e(" />")]),e(`
    	`),s("span",{class:"hljs-tag"},[e("<"),s("span",{class:"hljs-name"},"button"),e(">")]),e("Send"),s("span",{class:"hljs-tag"},[e("</"),s("span",{class:"hljs-name"},"button"),e(">")]),e(`
	`),s("span",{class:"hljs-tag"},[e("</"),s("span",{class:"hljs-name"},"form"),e(">")]),e(`
	`),s("span",{class:"hljs-tag"},[e("<"),s("span",{class:"hljs-name"},"script"),e(),s("span",{class:"hljs-attr"},"src"),e("="),s("span",{class:"hljs-string"},'"/index.js"'),e(),s("span",{class:"hljs-attr"},"type"),e("="),s("span",{class:"hljs-string"},'"module"'),e(">")]),s("span",{class:"hljs-tag"},[e("</"),s("span",{class:"hljs-name"},"script"),e(">")]),e(`
`),s("span",{class:"hljs-tag"},[e("</"),s("span",{class:"hljs-name"},"body"),e(">")]),e(`
`),s("span",{class:"hljs-tag"},[e("</"),s("span",{class:"hljs-name"},"html"),e(">")]),e(`
`)])],-1),me=s("h2",{id:"folders"},[e("Folders "),s("a",{class:"header-anchor",href:"#folders"},"#")],-1),_e=s("pre",null,[s("code",{class:"language-js"},`directus.folders;
`)],-1),ge=s("p",null,[e("Same methods as "),s("code",null,'directus.items("directus_folders")'),e(".")],-1),fe=s("h2",{id:"permissions"},[e("Permissions "),s("a",{class:"header-anchor",href:"#permissions"},"#")],-1),ye=s("pre",null,[s("code",{class:"language-js"},`directus.permissions;
`)],-1),we=s("p",null,[e("Same methods as "),s("code",null,'directus.items("directus_permissions")'),e(".")],-1),ke=s("h2",{id:"presets"},[e("Presets "),s("a",{class:"header-anchor",href:"#presets"},"#")],-1),be=s("pre",null,[s("code",{class:"language-js"},`directus.presets;
`)],-1),ve=s("p",null,[e("Same methods as "),s("code",null,'directus.items("directus_presets")'),e(".")],-1),Se=s("h2",{id:"relations"},[e("Relations "),s("a",{class:"header-anchor",href:"#relations"},"#")],-1),De=s("pre",null,[s("code",{class:"language-js"},`directus.relations;
`)],-1),Te=s("p",null,[e("Same methods as "),s("code",null,'directus.items("directus_relations")'),e(".")],-1),xe=s("h2",{id:"revisions"},[e("Revisions "),s("a",{class:"header-anchor",href:"#revisions"},"#")],-1),Ie=s("pre",null,[s("code",{class:"language-js"},`directus.revisions;
`)],-1),Ae=s("p",null,[e("Same methods as "),s("code",null,'directus.items("directus_revisions")'),e(".")],-1),Re=s("h2",{id:"roles"},[e("Roles "),s("a",{class:"header-anchor",href:"#roles"},"#")],-1),Be=s("pre",null,[s("code",{class:"language-js"},`directus.roles;
`)],-1),Me=s("p",null,[e("Same methods as "),s("code",null,'directus.items("directus_roles")'),e(".")],-1),qe=s("h2",{id:"settings"},[e("Settings "),s("a",{class:"header-anchor",href:"#settings"},"#")],-1),Ce=s("pre",null,[s("code",{class:"language-js"},`directus.settings;
`)],-1),Pe=s("p",null,[e("Same methods as "),s("code",null,'directus.items("directus_settings")'),e(".")],-1),Ue=s("h2",{id:"server"},[e("Server "),s("a",{class:"header-anchor",href:"#server"},"#")],-1),Oe=s("h3",{id:"ping-the-server"},[e("Ping the Server "),s("a",{class:"header-anchor",href:"#ping-the-server"},"#")],-1),Ke=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(` directus.server.ping();
`)])],-1),Ee=s("h3",{id:"get-server%2Fproject-info"},[e("Get Server/Project Info "),s("a",{class:"header-anchor",href:"#get-server%2Fproject-info"},"#")],-1),Fe=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(` directus.server.info();
`)])],-1),Ne=s("h2",{id:"users"},[e("Users "),s("a",{class:"header-anchor",href:"#users"},"#")],-1),We=s("pre",null,[s("code",{class:"language-js"},`directus.users;
`)],-1),Le=s("p",null,[e("Same methods as "),s("code",null,'directus.items("directus_users")'),e(", and:")],-1),ze=s("h3",{id:"invite-a-new-user"},[e("Invite a New User "),s("a",{class:"header-anchor",href:"#invite-a-new-user"},"#")],-1),Ve=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" directus.users.invites.send("),s("span",{class:"hljs-string"},"'admin@example.com'"),e(", "),s("span",{class:"hljs-string"},"'fe38136e-52f7-4622-8498-112b8a32a1e2'"),e(`);
`)])],-1),Ge=s("p",null,"The second parameter is the role of the user",-1),Je=s("h3",{id:"accept-a-user-invite"},[e("Accept a User Invite "),s("a",{class:"header-anchor",href:"#accept-a-user-invite"},"#")],-1),Ye=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" directus.users.invites.accept("),s("span",{class:"hljs-string"},"'<accept-token>'"),e(", "),s("span",{class:"hljs-string"},"'n3w-p455w0rd'"),e(`);
`)])],-1),He=s("p",null,"The provided token is sent to the user\u2019s email",-1),$e=s("h3",{id:"enable-two-factor-authentication"},[e("Enable Two-Factor Authentication "),s("a",{class:"header-anchor",href:"#enable-two-factor-authentication"},"#")],-1),Qe=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" directus.users.tfa.enable("),s("span",{class:"hljs-string"},"'my-password'"),e(`);
`)])],-1),Xe=s("h3",{id:"disable-two-factor-authentication"},[e("Disable Two-Factor Authentication "),s("a",{class:"header-anchor",href:"#disable-two-factor-authentication"},"#")],-1),Ze=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" directus.users.tfa.disable("),s("span",{class:"hljs-string"},"'691402'"),e(`);
`)])],-1),st=s("h3",{id:"get-the-current-user"},[e("Get the Current User "),s("a",{class:"header-anchor",href:"#get-the-current-user"},"#")],-1),et=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(` directus.users.me.read();
`)])],-1),tt=s("p",null,"Supports optional query:",-1),at=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(` directus.users.me.read({
	`),s("span",{class:"hljs-attr"},"fields"),e(": ["),s("span",{class:"hljs-string"},"'last_access'"),e(`],
});
`)])],-1),nt=s("h3",{id:"update-the-current-users"},[e("Update the Current Users "),s("a",{class:"header-anchor",href:"#update-the-current-users"},"#")],-1),lt=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" directus.users.me.update({ "),s("span",{class:"hljs-attr"},"first_name"),e(": "),s("span",{class:"hljs-string"},"'Admin'"),e(` });
`)])],-1),ot=s("p",null,"Supports optional query:",-1),ct=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" directus.users.me.update({ "),s("span",{class:"hljs-attr"},"first_name"),e(": "),s("span",{class:"hljs-string"},"'Admin'"),e(" }, { "),s("span",{class:"hljs-attr"},"fields"),e(": ["),s("span",{class:"hljs-string"},"'last_access'"),e(`] });
`)])],-1),it=s("h2",{id:"utils"},[e("Utils "),s("a",{class:"header-anchor",href:"#utils"},"#")],-1),rt=s("h3",{id:"get-a-random-string"},[e("Get a Random String "),s("a",{class:"header-anchor",href:"#get-a-random-string"},"#")],-1),ht=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(` directus.utils.random.string();
`)])],-1),dt=s("p",null,[e("Supports an optional "),s("code",null,"length"),e(" (defaults to 32):")],-1),pt=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" directus.utils.random.string("),s("span",{class:"hljs-number"},"50"),e(`);
`)])],-1),ut=s("h3",{id:"generate-a-hash-for-a-given-value"},[e("Generate a Hash for a Given Value "),s("a",{class:"header-anchor",href:"#generate-a-hash-for-a-given-value"},"#")],-1),jt=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" directus.utils.hash.generate("),s("span",{class:"hljs-string"},"'My String'"),e(`);
`)])],-1),mt=s("h3",{id:"verify-if-a-hash-is-valid"},[e("Verify if a Hash is Valid "),s("a",{class:"header-anchor",href:"#verify-if-a-hash-is-valid"},"#")],-1),_t=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" directus.utils.hash.verify("),s("span",{class:"hljs-string"},"'My String'"),e(", "),s("span",{class:"hljs-string"},"'$argon2i$v=19$m=4096,t=3,p=1$A5uogJh'"),e(`);
`)])],-1),gt=s("h3",{id:"sort-items-in-a-collection"},[e("Sort Items in a Collection "),s("a",{class:"header-anchor",href:"#sort-items-in-a-collection"},"#")],-1),ft=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" directus.utils.sort("),s("span",{class:"hljs-string"},"'articles'"),e(", "),s("span",{class:"hljs-number"},"15"),e(", "),s("span",{class:"hljs-number"},"42"),e(`);
`)])],-1),yt=s("p",null,[e("This will move item "),s("code",null,"15"),e(" to the position of item "),s("code",null,"42"),e(", and move everything in between one \u201Cslot\u201D up.")],-1),wt=s("h3",{id:"revert-to-a-previous-revision"},[e("Revert to a Previous Revision "),s("a",{class:"header-anchor",href:"#revert-to-a-previous-revision"},"#")],-1),kt=s("pre",null,[s("code",{class:"language-js"},[s("span",{class:"hljs-keyword"},"await"),e(" directus.utils.revert("),s("span",{class:"hljs-number"},"451"),e(`);
`)])],-1),bt=s("p",null,"Note: The key passed is the primary key of the revision you\u2019d like to apply.",-1),vt=s("hr",null,null,-1),It={setup(St,{expose:l}){const t={title:"JavaScript SDK",modularExtension:!1};return l({frontmatter:t}),(Dt,Tt)=>{const o=a("router-link"),c=a("DocsWrapper");return i(),r(c,{frontmatter:t},{default:n(()=>[s("div",d,[p,u,j,m,_,g,f,y,w,k,b,v,S,D,T,x,I,A,R,B,M,q,C,P,U,O,K,E,F,N,W,L,z,V,G,s("div",J,[Y,s("p",null,[H,h(o,{to:"/docs/configuration/config-options/#security"},{default:n(()=>[$]),_:1}),Q])]),X,Z,ss,es,ts,as,ns,ls,os,cs,is,rs,hs,ds,ps,us,js,ms,_s,gs,fs,ys,ws,ks,bs,vs,Ss,Ds,Ts,xs,Is,As,Rs,Bs,Ms,qs,Cs,Ps,Us,Os,Ks,Es,Fs,Ns,Ws,Ls,zs,Vs,Gs,Js,Ys,Hs,$s,Qs,Xs,Zs,se,ee,te,ae,ne,le,oe,ce,ie,re,he,de,pe,ue,je,me,_e,ge,fe,ye,we,ke,be,ve,Se,De,Te,xe,Ie,Ae,Re,Be,Me,qe,Ce,Pe,Ue,Oe,Ke,Ee,Fe,Ne,We,Le,ze,Ve,Ge,Je,Ye,He,$e,Qe,Xe,Ze,st,et,tt,at,nt,lt,ot,ct,it,rt,ht,dt,pt,ut,jt,mt,_t,gt,ft,yt,wt,kt,bt,vt])]),_:1})}}};export{It as default};
