- Notifications
You must be signed in to change notification settings - Fork 1.2k
Add API Reference and Guides for TypeScript in docs #2266
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits Select commit Hold shift + click to select a range
0a8e42b Add API Reference and Guides for TypeScript in docs
Convly 910186b Add TypeScript guide for manipulating Strapi documents and entries
Convly 638b2e7 Add new tags to TypeScript development docs
Convly e613ccb Update docusaurus/docs/dev-docs/typescript/development/guides/documen…
pwizla da7e842 Update docusaurus/docs/dev-docs/typescript/development/guides/documen…
pwizla e797774 Update docusaurus/docs/dev-docs/typescript/development/guides/documen…
pwizla 7b41cef Update docusaurus/docs/dev-docs/typescript/development/guides/documen…
pwizla 4ea2f48 Update docusaurus/docs/dev-docs/typescript/development/guides/documen…
pwizla 3a92d21 Update docusaurus/docs/dev-docs/typescript/development/guides/documen…
pwizla 512e1fb Update docusaurus/docs/dev-docs/typescript/development/guides/documen…
pwizla 2813ffc Update docusaurus/docs/dev-docs/typescript/development/guides/documen…
pwizla 8f0b412 Update docusaurus/sidebars.js
pwizla 7c434b1 Update docusaurus/docs/dev-docs/typescript/development/guides/documen…
Convly 406a8d5 Update docusaurus/docs/dev-docs/typescript/development/guides/documen…
Convly 619142c Update docusaurus/docs/dev-docs/typescript/development/guides/documen…
Convly c0f23ce Update docusaurus/docs/dev-docs/typescript/development.md
Convly b26d5ce Update docusaurus/docs/dev-docs/typescript/development.md
Convly b77060c add summaries and fixes
innerdvations 42e6e3f remove link to api reference
innerdvations 38faf6d Update docusaurus/docs/dev-docs/typescript/development.md
innerdvations 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 hidden or 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 @@ | ||
| .idea/ |
This file contains hidden or 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
12 changes: 12 additions & 0 deletions 12 docusaurus/docs/dev-docs/typescript/development/api-reference.md
This file contains hidden or 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,12 @@ | ||
| --- | ||
| title: TypeScript - API Reference | ||
| sidebar_label: API Reference | ||
| description: API Reference for Strapi Type System | ||
| tags: | ||
| - typescript | ||
| - api | ||
| --- | ||
| | ||
| # API Reference | ||
| | ||
| :building_construction: **Still under construction, come back later** :construction: | ||
This file contains hidden or 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,18 @@ | ||
| --- | ||
| title: TypeScript - Guides | ||
| sidebar_label: Guides | ||
| description: List of TypeScript guides to get started with Strapi and TypeScript | ||
| tags: | ||
| - typescript | ||
| - guides | ||
| --- | ||
| | ||
| # TypeScript Guides | ||
| | ||
| This page includes a curated list of common examples useful to use types while [developing a Strapi TypeScript application](/dev-docs/typescript/development). | ||
| | ||
| <CustomDocCardsWrapper> | ||
| | ||
| <CustomDocCard emoji="📄" title="Manipulating Data" description="Manipulate documents and components in a type-safe manner." link="/dev-docs/typescript/development/guides/documents-and-entries"/> | ||
| | ||
| </CustomDocCardsWrapper> |
214 changes: 214 additions & 0 deletions 214 docusaurus/docs/dev-docs/typescript/development/guides/documents-and-entries.md
This file contains hidden or 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,214 @@ | ||
| --- | ||
| title: TypeScript - Manipulating Documents and Entries | ||
| sidebar_label: Manipulating Documents and Entries | ||
| description: TypeScript guide to get started with manipulating documents and entries | ||
| tags: | ||
| - typescript | ||
| - guides | ||
| - data | ||
| - document | ||
| - component | ||
| - uid | ||
| - entries | ||
| --- | ||
| | ||
| # Manipulating documents and entries | ||
innerdvations marked this conversation as resolved. Show resolved Hide resolved | ||
| | ||
| This guide will explore TypeScript patterns for manipulating documents and entries in a Strapi v5 application, including how to leverage Strapi's `UID` and `Data` namespaces to interact with both generic and known entity types safely. If you're working on a TypeScript-based Strapi project, mastering these approaches will help you take full advantage of type safety and code completion, ensuring robust, error-free interactions with your application’s content and components. | ||
| | ||
| :::prerequisites | ||
| | ||
| - **Strapi Application:** A Strapi v5 application. If you don't have one, follow the [documentation](/dev-docs/quick-start) to get started. | ||
| - **TypeScript:** Ensure TypeScript is set up in your Strapi project. You can follow Strapi's [official guide](/dev-docs/typescript#getting-started-with-typescript-in-strapi) on configuring TypeScript. | ||
| - **Generated Types:** Application types [have been generated](/dev-docs/typescript/development#generate-typings-for-content-types-schemas) and are accessible. | ||
| ::: | ||
| | ||
Convly marked this conversation as resolved. Show resolved Hide resolved | ||
| ## Type Imports | ||
| | ||
| The `UID` namespace contains literal unions representing the available resources in the application. | ||
| | ||
| ```typescript | ||
| import type { UID } from '@strapi/strapi'; | ||
| ``` | ||
| | ||
| - `UID.ContentType` represents a union of every content-type identifier in the application | ||
| - `UID.Component` represents a union of every component identifier in the application | ||
| - `UID.Schema` represents a union of every schema (content-type or component) identifier in the application | ||
| - And others... | ||
| | ||
| --- | ||
| | ||
| Strapi provides a `Data` namespace containing several built-in types for entity representation. | ||
| | ||
| ```typescript | ||
| import type { Data } from '@strapi/strapi'; | ||
| ``` | ||
| | ||
| - `Data.ContentType` represents a Strapi document object | ||
| - `Data.Component` represents a Strapi component object | ||
| - `Data.Entity` represents either a document or a component | ||
| | ||
| :::tip | ||
| Both the entities' type definitions and UIDs are based on the generated schema types for your application. | ||
| | ||
| In case of a mismatch or error, you can always [regenerate the types](/dev-docs/typescript/development#generate-typings-for-content-types-schemas). | ||
| ::: | ||
| | ||
| ## Usage | ||
| | ||
| ### Generic entities | ||
| | ||
| When dealing with generic data, it is recommended to use non-parametrized forms of the `Data` types. | ||
| | ||
| #### Generic documents | ||
| | ||
| ```typescript | ||
| async function save(name: string, document: Data.ContentType) { | ||
| await writeCSV(name, document); | ||
| // ^ { | ||
Convly marked this conversation as resolved. Show resolved Hide resolved | ||
| // id: Data.ID; | ||
| // documentId: string; | ||
| // createdAt?: DateTimeValue; | ||
| // updatedAt?: DateTimeValue; | ||
| // publishedAt?: DateTimeValue; | ||
| // ... | ||
| // } | ||
| } | ||
| ``` | ||
| | ||
| :::warning | ||
| In the preceding example, the resolved properties for `document` are those common to every content-type. | ||
| | ||
| Other properties have to be checked manually using type guards. | ||
| | ||
| ```typescript | ||
| if ('my_prop' in document) { | ||
| return document.my_prop; | ||
| } | ||
| ``` | ||
| | ||
| ::: | ||
| | ||
| #### Generic components | ||
| | ||
| ```typescript | ||
| function renderComponent(parent: Node, component: Data.Component) { | ||
| const elements: Element[] = []; | ||
| const properties = Object.entries(component); | ||
| | ||
| for (const [name, value] of properties) { | ||
| // ^ ^ | ||
| // string any | ||
| const paragraph = document.createElement('p'); | ||
| | ||
| paragraph.textContent = `Key: ${name}, Value: ${value}`; | ||
| | ||
| elements.push(paragraph); | ||
| } | ||
| | ||
| parent.append(...elements); | ||
| } | ||
| ``` | ||
| | ||
| ### Known entities | ||
| | ||
| When manipulating known entities, it is possible to parametrize `Data` types for better type safety and code completion. | ||
| | ||
| #### Known documents | ||
| | ||
| ```typescript | ||
| const ALL_CATEGORIES = ['food', 'tech', 'travel']; | ||
| | ||
| function validateArticle(article: Data.ContentType<'api::article.article'>) { | ||
| const { title, category } = article; | ||
| // ^? ^? | ||
| // string Data.ContentType<'api::category.category'> | ||
| | ||
| if (title.length < 5) { | ||
| throw new Error('Title too short'); | ||
| } | ||
| | ||
| if (!ALL_CATEGORIES.includes(category.name)) { | ||
| throw new Error(`Unknown category ${category.name}`); | ||
| } | ||
| } | ||
| ``` | ||
| | ||
| #### Known components | ||
| | ||
| ```typescript | ||
| function processUsageMetrics( | ||
| id: string, | ||
| metrics: Data.Component<'app.metrics'> | ||
| ) { | ||
| telemetry.send(id, { clicks: metrics.clicks, views: metrics.views }); | ||
| } | ||
| ``` | ||
| | ||
| ### Advanced use cases | ||
| | ||
| #### Entities subsets | ||
| | ||
| Using the types' second parameter (`TKeys`), it is possible to obtain a subset of an entity. | ||
| | ||
| ```typescript | ||
| type Credentials = Data.ContentType<'api::acount.acount', 'email' | 'password'>; | ||
| // ^? { email: string; password: string } | ||
| ``` | ||
| | ||
| ```typescript | ||
| type UsageMetrics = Data.Component<'app.metrics', 'clicks' | 'views'>; | ||
| // ^? { clicks: number; views: number } | ||
| ``` | ||
| | ||
| #### Type argument inference | ||
| | ||
| It is possible to bind and restrict an entity type based on other function parameters. | ||
| | ||
| In the following example, the `uid` type is inferred upon usage as `T` and used as a type parameter for the `document`. | ||
| | ||
| ```typescript | ||
| import type { UID } from '@strapi/strapi'; | ||
| | ||
| function display<T extends UID.ContentType>( | ||
| uid: T, | ||
| document: Data.ContentType<T> | ||
| ) { | ||
| switch (uid) { | ||
| case 'api::article.article': { | ||
| return document.title; | ||
| // ^? string | ||
| // ^? Data.ContentType<'api::article.article'> | ||
| } | ||
| case 'api::category.category': { | ||
| return document.name; | ||
| // ^? string | ||
| // ^? Data.ContentType<'api::category.category'> | ||
| } | ||
| case 'api::account.account': { | ||
| return document.email; | ||
| // ^? string | ||
| // ^? Data.ContentType<'api::account.account'> | ||
| } | ||
| default: { | ||
| throw new Error(`unknown content-type uid: "${uid}"`); | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
| | ||
| When calling the function, the `document` type needs to match the given `uid`. | ||
| | ||
| ```typescript | ||
| declare const article: Data.Document<'api::article.article'>; | ||
| declare const category: Data.Document<'api::category.category'>; | ||
| declare const account: Data.Document<'api::account.account'>; | ||
| | ||
| display('api::article.article', article); | ||
| display('api::category.category', category); | ||
| display('api::account.account', account); | ||
| // ^ ✅ | ||
| | ||
| display('api::article.article', category); | ||
| // ^ Error: "category" is not assignable to parameter of type ContentType<'api::article.article'> | ||
| ``` | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Until this page has some more content, I'd suggest we don't link to it from the sidebar and the TypeScript intro. page.
If you want to inform users that an API reference is in the works, I'd suggest you use the special
:::strapicallout in the TypeScript development intro. page.See my comment in the TypeScript development file :)
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.
Sorry, just taking over here while @Convly is off and don't fully understand what we would need to change. Could you update it the way you think would work best?