استيراد الأنواع
كل واجهة ونوع عام في SDK يُعاد تصديره من جذر الحزمة ومن نقطة دخول مخصصة oneentry/types. لا تحتاج إلى مسارات عميقة oneentry/dist/....
باختصار
// before — deep path into the build output
import type { IAttributeSchemaItem, IAttributeSetsEntity } from 'oneentry/dist/attribute-sets/attributeSetsInterfaces';
// now — either of these
import type { IAttributeSchemaItem, IAttributeSetsEntity } from 'oneentry';
import type { IAttributeSchemaItem, IAttributeSetsEntity } from 'oneentry/types';
تكشف نقطتا الدخول مجموعة الأنواع نفسها: كل واجهة ونوع في كل وحدة، بما في ذلك الأنواع المشتركة من base/utils (IError، IAttributeValues، ILocalizeInfo، IConfig، …).
أي نقطة دخول أستخدم؟
| استورد من | استخدمها عندما |
|---|---|
'oneentry' | تستورد بالفعل defineOneEntry (أو دالة مساعدة) من الجذر، فيغطي سطر استيراد واحد القيم والأنواع. |
'oneentry/types' | تريد وحدة للأنواع فقط. لا تحتوي على كود تشغيلي، مما يُبقي الملفات الخاصة بالأنواع (*.d.ts، وملفات النماذج المشتركة، وعقود API) خالية من استيرادات القيم. |
تعملان بالطريقة نفسها؛ اختر ما يُقرأ بشكل أفضل في مشروعك.
الجمع بين القيم والأنواع
تُصدّر نقطة الدخول الجذرية كلًا من واجهة التشغيل والأنواع، لذا يكفي غالبًا سطر واحد:
import { defineOneEntry } from 'oneentry';
import type { IConfig, IError, IProductsResponse } from 'oneentry';
const config: IConfig = { token: 'your-app-token' };
const api = defineOneEntry('https://your-project.oneentry.cloud', config);
const products: IProductsResponse | IError = await api.Products.getProducts();
استخدم import type (أو import { type X }) للأنواع. يُمحى الاستيراد وقت الترجمة، فلا يضيف شيئًا إلى حزمتك.
الاستيرادات العميقة لا تزال تعمل
تستمر الاستيرادات من oneentry/dist/<module>/<module>Interfaces في الترجمة. نقطتا الدخول الجذرية وoneentry/types إضافات، فيمكنك الترحيل ملفًا بعد ملف.
// still valid
import type { IOrderData } from 'oneentry/dist/orders/ordersInterfaces';
من أين تأتي الأنواع؟
تساهم كل وحدة في SDK بواجهاتها الخاصة، وجميعها متاحة من نقطتي الدخول:
| الوحدة | الأنواع النموذجية |
|---|---|
| Products | IProductsEntity, IProductsResponse, IProductsQueryBase, IFilterParams |
| Pages | IPagesEntity, IPositionBlock, PageType |
| Blocks | IBlockEntity, BlockType, IBlockSlidesResponse |
| Orders | IOrderData, IBaseOrdersEntity, IOrderByMarkerEntity, IRefundRequest |
| Users | IUserEntity, ICartResponse, IWishlistResponse |
| Forms / FormData | IFormsEntity, IFormAttribute, IBodyPostFormData, IFormsDataFilter, FormDataStatus |
| AttributesSets | IAttributeSetsEntity, IAttributeSchemaItem |
| Search | IGlobalSearchResponse, IGlobalSearchGroup, TGlobalSearchEntityType |
مشتركة (base/utils) | IError, IConfig, IAttributeValues, IAttributeValue, IAttributeFile, ITypedAttributeValue, ILocalizeInfo, LangType, IVectorSearchResponse |
القائمة الكاملة لأنواع الوحدة موثقة في صفحة مقدمة تلك الوحدة.
التحقق من الأخطاء
IError هو النوع الذي يقوم عليه الوضع الافتراضي لـ SDK "الأخطاء كقيم" (isShell: true). استورده من الجذر مثل أي نوع آخر:
import type { IError } from 'oneentry';
function isErrorResult(result: unknown): result is IError {
return (
!!result &&
typeof result === 'object' &&
('statusCode' in result || 'message' in result)
);
}
const result = await api.Products.getProducts();
if (isErrorResult(result)) {
console.error('Error:', result.message);
} else {
console.log('Success:', result);
}
ذات صلة
- حجم الحزمة وصيغ الوحدات: كيف تُحزَّم SDK وما الذي يُحمَّل فعليًا
- البدء: التثبيت والإعداد