Mengimpor Tipe
Setiap antarmuka publik dan tipe dari SDK diekspor ulang dari akar paket dan dari titik masuk khusus oneentry/types, sehingga jalur dalam oneentry/dist/... yang dalam tidak lagi diperlukan.
Versi singkat
// 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';
Kedua titik masuk mengekspos set tipe yang sama — setiap antarmuka dan tipe dari setiap modul, termasuk yang dibagikan dari base/utils (IError, IAttributeValues, ILocalizeInfo, IConfig, …).
Titik masuk mana yang harus saya gunakan?
| Impor dari | Gunakan saat |
|---|---|
'oneentry' | Anda sudah mengimpor defineOneEntry (atau pembantu) dari akar — satu baris impor mencakup nilai dan tipe. |
'oneentry/types' | Anda menginginkan modul hanya tipe: ini tidak membawa kode runtime sama sekali, yang menjaga file-file hanya tipe (*.d.ts, file model bersama, kontrak API) bebas dari impor nilai. |
Tidak ada perbedaan fungsional antara keduanya — pilih yang lebih mudah dibaca di proyek Anda.
Mencampur nilai dan tipe
Titik masuk akar mengekspor baik API runtime maupun tipe, jadi satu baris sering kali cukup:
import { defineOneEntry } from 'oneentry';
import type { IConfig, IError, IProductsEntity } from 'oneentry';
const config: IConfig = { token: 'your-app-token' };
const api = defineOneEntry('https://my-project.oneentry.cloud', config);
const products: IProductsEntity[] | IError = await api.Products.getProducts();
Gunakan import type (atau import { type X }) untuk tipe: impor ini dihapus pada waktu kompilasi, sehingga tidak pernah berkontribusi apa pun pada bundel Anda.
Impor dalam masih berfungsi
Tidak ada yang dihapus. Kode yang ada yang mengimpor dari oneentry/dist/<module>/<module>Interfaces tetap dikompilasi persis seperti sebelumnya — titik masuk baru adalah tambahan, jadi Anda dapat memigrasi file demi file dengan kecepatan Anda sendiri.
// still valid
import type { IOrderData } from 'oneentry/dist/orders/ordersInterfaces';
Dari mana asal tipe-tipe ini?
Setiap modul SDK menyumbangkan antarmukanya sendiri, dan semuanya berakhir di bawah kedua titik masuk:
| Modul | Tipe tipikal |
|---|---|
| Products | IProductsEntity, IProductsResponse, IProductsQueryBase, IFilterParams |
| Pages | IPagesEntity, IPositionBlock, PageType |
| Blocks | IBlocksEntity, BlockType, IContentSlidesResponse |
| Orders | IOrderData, IBaseOrdersEntity, IOrderByMarkerEntity, IRefundRequest |
| Users | IUserEntity, ICartResponse, IWishlistResponse |
| Forms / FormData | IFormsEntity, IFormAttribute, IBodyPostFormData |
| AttributesSets | IAttributeSetsEntity, IAttributeSchemaItem |
Bersama (base/utils) | IError, IConfig, IAttributeValues, IAttributeValue, ILocalizeInfo, LangType |
Daftar lengkap tipe dari sebuah modul didokumentasikan di halaman pengantar modul tersebut.
Memeriksa kesalahan
IError adalah tipe di balik mode default "kesalahan sebagai nilai" SDK (isShell: true), dan tersedia dari akar seperti yang lainnya:
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);
}
🔗 Dokumentasi Terkait
- Ukuran Bundel & Format Modul - bagaimana SDK dikemas dan apa yang sebenarnya dimuat
- Mulai - instalasi dan konfigurasi