Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 12 additions & 45 deletions demo/node-typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { strapi } from '@strapi/client';
import { strapi, type API } from '@strapi/client';
import * as dotenv from 'dotenv';
import * as os from 'os';
dotenv.config();
Expand All @@ -11,65 +11,32 @@ console.log('Running with api token ' + api_token);
const client = strapi({ baseURL: 'http://localhost:1337/api', auth: api_token });

// Type definitions based on the actual response structure
interface CategoryImage {
interface CategoryImage extends API.Document {
id: number;
name: string;
alternativeText: string | null;
caption: string | null;
alternativeText?: string;
caption?: string;
width: number;
height: number;
formats: Record<string, unknown>;
formats?: Record<string, unknown>;
hash: string;
ext: string;
mime: string;
size: number;
url: string;
previewUrl: string | null;
provider: string;
createdAt: string;
updatedAt: string;
}

interface Category {
interface Category extends API.Document {
id: number;
name: string;
slug: string;
image?: CategoryImage;
createdAt: string;
updatedAt: string;
publishedAt: string | null;
}

interface CategoryResponse {
data: Category[];
meta: {
pagination: {
page: number;
pageSize: number;
pageCount: number;
total: number;
};
};
}

interface FileAttributes {
id: number;
name: string;
alternativeText: string | null;
caption: string | null;
width: number;
height: number;
formats: Record<string, unknown>;
hash: string;
ext: string;
mime: string;
size: number;
url: string;
previewUrl: string | null;
provider: string;
createdAt: string;
updatedAt: string;
}
type CategoryResponseCollection = API.DocumentResponseCollection<Category>;

async function runDemo() {
await demonstrateBasicCategoryFunctionality();
Expand All @@ -86,7 +53,7 @@ async function demonstrateBasicCategoryFunctionality() {

const categories = client.collection('categories');

const categoryDocs = (await categories.find()) as unknown as CategoryResponse;
const categoryDocs = (await categories.find()) as CategoryResponseCollection;

console.log(`Found ${categoryDocs.data.length} categories:`);
categoryDocs.data.forEach((category) => {
Expand All @@ -104,7 +71,7 @@ async function demonstrateCategoryImageInteractions() {
// Fetch all categories with their related images
const result = (await categories.find({
populate: ['image'],
})) as unknown as CategoryResponse;
})) as CategoryResponseCollection;

for (const category of result.data) {
console.log(`Category: ${category.name}`);
Expand Down Expand Up @@ -136,7 +103,7 @@ async function demonstrateDirectFileOperations() {
},
},
populate: ['image'],
})) as unknown as CategoryResponse;
})) as CategoryResponseCollection;

if (techCategoryResult.data && techCategoryResult.data.length > 0) {
const categoryData = techCategoryResult.data[0];
Expand All @@ -147,7 +114,7 @@ async function demonstrateDirectFileOperations() {
const imageId = categoryData.image.id;

// Get the specific file by ID
const fileInfo = (await client.files.findOne(imageId)) as unknown as FileAttributes;
const fileInfo = await client.files.findOne(imageId);

console.log(os.EOL);
console.log('File details:');
Expand Down Expand Up @@ -178,7 +145,7 @@ async function demonstrateFileUpdates() {
},
},
populate: ['image'],
})) as unknown as CategoryResponse;
})) as CategoryResponseCollection;

if (techCategoryResult.data && techCategoryResult.data.length > 0) {
const categoryData = techCategoryResult.data[0];
Expand Down
23 changes: 10 additions & 13 deletions src/content-types/collection/manager.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import createDebug from 'debug';

import { HttpClient } from '../../http';
import {
GenericDocumentResponse,
GenericMultiDocumentResponse,
BaseQueryParams,
} from '../../types/content-api';
import { URLHelper } from '../../utilities';

import type * as API from '../../types/content-api';

const debug = createDebug('strapi:ct:collection');

/**
Expand Down Expand Up @@ -64,7 +61,7 @@ export class CollectionTypeManager {
* console.log(articles);
* ```
*/
async find(queryParams?: BaseQueryParams): Promise<GenericMultiDocumentResponse> {
async find(queryParams?: API.BaseQueryParams): Promise<API.DocumentResponseCollection> {
debug('finding documents for %o', this._pluralName);

let url = `/${this._pluralName}`;
Expand Down Expand Up @@ -105,8 +102,8 @@ export class CollectionTypeManager {
*/
async findOne(
documentID: string,
queryParams?: BaseQueryParams
): Promise<GenericDocumentResponse> {
queryParams?: API.BaseQueryParams
): Promise<API.DocumentResponse> {
debug('finding a document for %o with id: %o', this._pluralName, documentID);

let url = `/${this._pluralName}/${documentID}`;
Expand Down Expand Up @@ -142,8 +139,8 @@ export class CollectionTypeManager {
*/
async create(
data: Record<string, any>,
queryParams?: BaseQueryParams
): Promise<GenericDocumentResponse> {
queryParams?: API.BaseQueryParams
): Promise<API.DocumentResponse> {
debug('creating a document for %o', this._pluralName);

let url = `/${this._pluralName}`;
Expand Down Expand Up @@ -194,8 +191,8 @@ export class CollectionTypeManager {
async update(
documentID: string,
data: Record<string, unknown>,
queryParams?: BaseQueryParams
): Promise<GenericDocumentResponse> {
queryParams?: API.BaseQueryParams
): Promise<API.DocumentResponse> {
debug('updating a document for %o with id: %o', this._pluralName, documentID);

let url = `/${this._pluralName}/${documentID}`;
Expand Down Expand Up @@ -240,7 +237,7 @@ export class CollectionTypeManager {
* );
* ```
*/
async delete(documentID: string, queryParams?: BaseQueryParams): Promise<void> {
async delete(documentID: string, queryParams?: API.BaseQueryParams): Promise<void> {
debug('deleting a document for %o with id: %o', this._pluralName, documentID);

let url = `/${this._pluralName}/${documentID}`;
Expand Down
11 changes: 6 additions & 5 deletions src/content-types/single/manager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import createDebug from 'debug';

import { HttpClient } from '../../http';
import { BaseQueryParams, GenericDocumentResponse } from '../../types/content-api';
import { URLHelper } from '../../utilities';

import type * as API from '../../types/content-api';

const debug = createDebug('strapi:ct:single');

/**
Expand Down Expand Up @@ -60,7 +61,7 @@ export class SingleTypeManager {
* const localizedHomepage = await homepageManager.find({ locale: 'es' });
* ```
*/
async find(queryParams?: BaseQueryParams): Promise<GenericDocumentResponse> {
async find(queryParams?: API.BaseQueryParams): Promise<API.DocumentResponse> {
debug('finding document for %o', this._singularName);

let path = `/${this._singularName}`;
Expand Down Expand Up @@ -103,8 +104,8 @@ export class SingleTypeManager {
*/
async update(
data: Record<string, any>,
queryParams?: BaseQueryParams
): Promise<GenericDocumentResponse> {
queryParams?: API.BaseQueryParams
): Promise<API.DocumentResponse> {
debug('updating document for %o', this._singularName);

let url = `/${this._singularName}`;
Expand Down Expand Up @@ -150,7 +151,7 @@ export class SingleTypeManager {
* @see HttpClient
* @see URLHelper.appendQueryParams
*/
async delete(queryParams?: BaseQueryParams): Promise<void> {
async delete(queryParams?: API.BaseQueryParams): Promise<void> {
debug('deleting document for %o', this._singularName);

let url = `/${this._singularName}`;
Expand Down
1 change: 1 addition & 0 deletions src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export * from './errors';
export type { StrapiClientConfig, StrapiClient } from './client';
export type { CollectionTypeManager, SingleTypeManager } from './content-types';
export type { FilesManager, FileQueryParams, FileResponse, FileListResponse } from './files';
export type * as API from './types/content-api';

// ############################
// # Deprecated symbols #
Expand Down
16 changes: 9 additions & 7 deletions src/types/content-api.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
interface DocumentData {
export interface Document {
documentId: string;
createdAt: string;
updatedAt: string;
[key: string]: any;
}

interface Pagination {
export interface Pagination {
page: number;
pageSize: number;
pageCount: number;
total: number;
}

interface Meta {
export interface ResponseMeta {
pagination?: Pagination;
}

interface GenericResponse<T> {
export interface DocumentResponse<T extends Document = Document> {
data: T;
meta: Meta;
meta: ResponseMeta;
}

export type GenericDocumentResponse = GenericResponse<DocumentData>;
export type GenericMultiDocumentResponse = GenericResponse<DocumentData[]>;
export interface DocumentResponseCollection<T extends Document = Document> {
data: T[];
meta: ResponseMeta;
}

/**
* Defines the structure of query parameters supported for Strapi content API requests.
Expand Down