Skip to content

Instantly share code, notes, and snippets.

@wmakeev
Last active September 2, 2024 03:47
Show Gist options
  • Save wmakeev/e3c2098fabc17f980b27f31c0a007679 to your computer and use it in GitHub Desktop.
Save wmakeev/e3c2098fabc17f980b27f31c0a007679 to your computer and use it in GitHub Desktop.
[OZON JS API wrapper rev.2] #ozon #api
import { request } from "undici";
import nodePath from "node:path";
const OZON_API_ENDPOINT = "api-seller.ozon.ru";
export class Ozon {
/** @type {OzonOptions} */
#options;
/**
* Параметры инициализации класса
* @param {OzonOptions} options
*/
constructor(options) {
this.#options = options;
}
/**
*
* @param {import('undici').Dispatcher.HttpMethod} method
* @param {string} path
* @param {string} body
* @returns
*/
async fetchApi(method, path, body) {
const { apiKey, clientId, endpoint } = this.#options;
const url = nodePath.join(`https://${endpoint ?? OZON_API_ENDPOINT}`, path);
const resp = await request(url, {
method,
headers: {
"Client-Id": clientId,
"Api-Key": apiKey,
},
body: body ?? null,
});
const data = await resp.body.json();
if (resp.statusCode !== 200) {
throw new Error(data.message || `Ошибка запроса ${resp.statusCode}`);
}
return data;
}
/**
* POST Запос к API
* @param {string} path
* @param {string} body
*/
async POST(path, body) {
return await this.fetchApi("POST", path, body);
}
/**
* Список необработанных отправлений (версия 3)
*
* @param {V3GetFbsUnfulfilledListRequest} params
*/
async getPostingFbsUnfulfilledListV3(params) {
return this.POST(
"/v3/posting/fbs/unfulfilled/list",
JSON.stringify(params)
);
}
/**
* Список необработанных отправлений (версия 3)
*
* @param {V3GetFbsPostingListRequestFilter} params
* @returns {Promise<V3GetFbsPostingListResponse>}
*/
async getPostingFbsListV3(params) {
return this.POST("/v3/posting/fbs/list", JSON.stringify(params));
}
/**
* Список необработанных отправлений (версия 3)
*
* @param {V3FbsPostingGetParams} params
* @returns {Promise<V3GetFbsPostingResponse>}
*/
async getPostingFbsV3(params) {
return this.POST("/v3/posting/fbs/get", JSON.stringify(params));
}
}
interface OzonOptions {
clientId: string;
apiKey: string;
endpoint?: string;
}
/**
* @link https://docs.ozon.ru/api/seller/#operation/PostingAPI_GetFbsPostingUnfulfilledList
*/
interface V3GetFbsUnfulfilledListRequest {
dir?: "asc" | "desc";
filter: {
cutoff_from?: string;
cutoff_to?: string;
delivery_method_id?: [];
provider_id?: [];
status?: "awaiting_packaging";
warehouse_id?: [];
};
limit?: 100;
offset?: 0;
with?: {
analytics_data?: true;
barcodes?: true;
financial_data?: true;
translit?: true;
};
}
/**
* @link https://docs.ozon.ru/api/seller/#operation/PostingAPI_GetFbsPostingListV3
*/
interface V3GetFbsPostingListRequestFilter {
dir?: "asc" | "desc";
filter: {
delivery_method_id?: number;
order_id?: number;
provider_id?: number;
since?: string;
to?: string;
status?: [];
warehouse_id?: number;
};
limit?: number;
offset?: number;
with?: {
analytics_data?: true;
barcodes?: true;
financial_data?: true;
translit?: true;
};
}
interface V3FbsPostingGetParams {
posting_number: string;
with?: {
barcodes?: boolean;
related_postings?: boolean;
};
}
interface V3FbsPostingProduct {
mandatory_mark: string[];
name: string;
offer_id: string;
price: string;
quantity: number;
sku: number;
currency_code: "RUB";
}
interface V3FbsPosting {
posting_number: string;
order_id: number;
customer: V3CustomerFbs;
delivery_method: {
warehouse: string;
};
products: V3FbsPostingProduct[];
}
interface V3FbsPostingDetail {
addressee: {
name: string;
phone: string;
};
}
interface V3Address {
/** Адрес в текстовом формате */
address_tail: string;
zip_code: string;
country: string;
region: string;
city: string;
district: string;
latitude: number;
longitude: number;
provider_pvz_code: string;
pvz_code: number;
comment: string;
}
interface V3CustomerFbs {
customer_id: number;
name: string;
phone: string;
address: V3Address;
customer_email: string;
}
interface V3GetFbsPostingUnfulfilledListResponse {
result: {
postings: V3FbsPosting[];
count: number;
};
}
interface V3GetFbsPostingListResponse {
result: {
has_next: boolean;
postings: V3FbsPosting[];
};
}
interface V3GetFbsPostingListResponse {
result: {
has_next: boolean;
postings: V3FbsPosting[];
};
}
/**
* @link https://docs.ozon.ru/api/seller/#operation/PostingAPI_GetFbsPostingV3
*/
interface V3GetFbsPostingResponse {
result: V3FbsPosting;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment