البدء

ESM وCJS

تستخدم الحزمة تنسيق ESM. وتتوفر نسخة CommonJS للاستخدام مع require، وهي حزمة مجمّعة وليست مجرد نسخة محوّلة من الشيفرة. لذلك يجب الانتباه إلى طريقة التحميل بعد تعديل المصدر.

type: moduleنقطتا دخولدون اعتماديات أثناء التشغيل
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

نقاط الدخول

{
  "type": "module",
  "main": "./dist/index.cjs",
  "exports": {
    ".": {
      "import": "./src/index.js",     // ESM gets the source
      "require": "./dist/index.cjs"   // CJS gets the bundle
    }
  },
  "engines": { "node": ">=18" }
}
# Python has no ESM/CommonJS split: one package, one import path.
# Your own pyproject.toml only declares the dependency:
#
# [project]
# name = "my-app"
# requires-python = ">=3.10"
# dependencies = ["iagenify>=0.1.0"]
from iagenify import IaGenify

يقرأ import الشيفرة المصدرية المنشورة، فتشير تتبعات الاستدعاء إلى الملفات الفعلية داخل الحزمة. أما require فيقرأ ملفًا واحدًا جرى تجميعه مسبقًا.

تبقى وحدات Node المدمجة خارج الحزمة

لا تُضمّن أيٌّ من نقطتي الدخول الوحدات node:fs أو node:os أو node:path أو node:crypto أو node:child_process. تُستورد هذه الوحدات أثناء التشغيل بعد التحقق من البيئة، مما يسمح بتحميل الملف نفسه في بيئات لا تتوفر فيها تلك الوحدات: يفشل الاستيراد ببساطة، وتستخدم حزمة SDK البديل المناسب لبيئة التشغيل.

يظهر الأثر العملي في إعدادات أداة التجميع، لا في شيفرتك. راجع التثبيت.

الجمع بين التنسيقين

في ملف CommonJS، تحصل على الصادرات المسمّاة من الكائن المُعاد. وفي ملف ESM، تعمل الاستيرادات المسمّاة مباشرةً. يوفّر كلاهما الفئات نفسها، لذا يعمل instanceof ما دامت العملية الواحدة تحمّل إحدى النسختين فقط.

// CommonJS
const { IaGenify, NotEnoughCredits } = require("iagenify");

// CommonJS reaching for the ESM entry point, when you need it
const { IaGenify } = await import("iagenify");
# Python has one import statement for every kind of script
from iagenify import IaGenify, NotEnoughCredits

# Importing lazily, when you need it
import importlib
iagenify = importlib.import_module("iagenify")
لا تحمّل النسختين في العملية نفسها

القيمة التي تُطلقها نسخة ESM ليست مثيلًا لفئة نسخة CJS عند فحصها باستخدام instanceof. ومع ذلك، يظل الخطأ يحمل الحقول name وstatus وcode، ويمكن فحصها إذا تعذّر تجنّب الجمع بين النسختين.

TypeScript

لا تتضمن الحزمة تصريحات للأنواع. مع moduleResolution: "bundler" أو "node16"، يُحلّ مسار الاستيراد، لكن بنى البيانات تبقى دون تعريفات للأنواع.

// iagenify.d.ts — enough to stop the compiler complaining
declare module "iagenify" {
  export class IaGenify {
    constructor(options: {
      apiKey?: string; baseUrl?: string; storage?: unknown;
      sessionPath?: string; appName?: string; timeout?: number;
    });
    [key: string]: any;
  }
  export class IaGenifyError extends Error { status: number; code: string; details?: unknown }
  export class Unauthorized extends IaGenifyError {}
  export class NotEnoughCredits extends IaGenifyError { required: number; available: number }
}
# Type checkers read the package's Python signatures directly:
# no separate declaration file is needed.
from iagenify import IaGenify, IaGenifyError, Unauthorized, NotEnoughCredits

api: IaGenify = IaGenify(api_key=api_key, base_url=None, session_path=None, app_name=None, timeout=60)

try:
    api.get_user()
except NotEnoughCredits as error:      # error.required, error.available
    ...
except IaGenifyError as error:         # error.status, error.code, error.details
    ...