UNPKG

@innovatespace/ig-business

Version:
208 lines (200 loc) 7.41 kB
'use strict'; function _extends() { _extends = Object.assign || function assign(target) { for(var i = 1; i < arguments.length; i++){ var source = arguments[i]; for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key]; } return target; }; return _extends.apply(this, arguments); } const ENDPOINTS = { oauth: 'https://www.instagram.com/oauth/authorize', accessExchange: 'https://api.instagram.com/oauth/access_token', longLivedTokenExchange: 'https://graph.instagram.com/access_token', refreshEndPoint: 'https://graph.instagram.com/refresh_access_token', publishEndPoint: 'https://graph.instagram.com', accountEndPoint: 'https://graph.instagram.com' }; const requestHelper = async ({ body, url, method, headers, format = 'json' })=>{ const response = await fetch(url, { method, headers, body }); let errorMessage = `Request failed with status ${response.status}`; if (!response.ok) { const contentType = response.headers.get('content-type'); try { if (contentType == null ? void 0 : contentType.includes('application/json')) { const errorBody = await response.json(); errorMessage = (errorBody == null ? void 0 : errorBody.message) || JSON.stringify(errorBody); } else { errorMessage = await response.text(); } } catch (err) { // fallback to status text if parsing fails errorMessage = response.statusText || errorMessage || (err == null ? void 0 : err.message); } throw new Error(errorMessage); } if (format === 'text') { return await response.text(); } else { return await response.json(); } }; class InstagramPublish { async createContainer(dto) { if (!(dto == null ? void 0 : dto.image_url) && !(dto == null ? void 0 : dto.video_url)) { throw new Error('image_url or video_url is required'); } if ((dto == null ? void 0 : dto.image_url) && (dto == null ? void 0 : dto.video_url)) { throw new Error('Please provide either image_url or video_url'); } let dtoCopy = {}; if (dto == null ? void 0 : dto.video_url) { dtoCopy = _extends({}, dto, (dto == null ? void 0 : dto.upload_type) && { upload_type: dto.upload_type }, { media_type: dto.media_type || 'VIDEO', is_carousel_item: dto.is_carousel_item || false, video_url: dto.video_url }); } if (dto == null ? void 0 : dto.image_url) { dtoCopy = _extends({}, dto, (dto == null ? void 0 : dto.upload_type) && { upload_type: dto.upload_type }, { is_carousel_item: dto.is_carousel_item || false, image_url: dto.image_url }); } const url = `${ENDPOINTS.publishEndPoint}/${this.version}/${this.accountId}/media`; const response = await requestHelper({ url, method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${this.accessToken}` }, body: JSON.stringify(dtoCopy) }); return response; } async createCarouselContainer(dto) { const url = `${ENDPOINTS.publishEndPoint}/${this.version}/${this.accountId}/media`; const response = await requestHelper({ url, method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${this.accessToken}` }, body: JSON.stringify(dto) }); return response; } async publishContainer(creationId) { const url = `${ENDPOINTS.publishEndPoint}/${this.version}/${this.accountId}/media_publish`; const response = await requestHelper({ url, method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${this.accessToken}` }, body: JSON.stringify({ creation_id: creationId }) }); return response; } constructor(accessToken, accountId, version = 'v23.0'){ this.accessToken = accessToken; this.accountId = accountId; this.version = version; } } class OAuthInstagram { static getRedirectUri({ scope, clientId, redirectUri, force_reauth, state }) { const params = new URLSearchParams(_extends({ client_id: clientId, redirect_uri: redirectUri, response_type: 'code', scope: scope.join(',') }, force_reauth && { force_reauth: 'true' }, state && { state })); return `${ENDPOINTS.oauth}?${params.toString()}`; } async getShortLivedInstagramAccessToken(redirectUri, code) { const formData = new FormData(); formData.append('client_id', this.clientId); formData.append('client_secret', this.clientSecret); formData.append('grant_type', 'authorization_code'); formData.append('redirect_uri', redirectUri); formData.append('code', code); const response = await requestHelper({ url: ENDPOINTS.accessExchange, method: 'POST', body: formData }); return response; } async getLongLivedInstagramAccessToken(shortLivedAccessToken) { const params = new URLSearchParams({ grant_type: 'ig_exchange_token', client_secret: this.clientSecret, access_token: shortLivedAccessToken }); const url = `${ENDPOINTS.longLivedTokenExchange}?${params.toString()}`; const response = await requestHelper({ url, method: 'GET', format: 'json' }); return response; } async refreshInstagramAccessToken(longLivedAccessToken) { const params = new URLSearchParams({ grant_type: 'ig_refresh_token', access_token: longLivedAccessToken }); const url = `${ENDPOINTS.refreshEndPoint}?${params.toString()}`; const response = await requestHelper({ url, method: 'GET', format: 'json' }); return response; } constructor(clientId, clientSecret){ this.clientId = clientId; this.clientSecret = clientSecret; } } class InstagramAccount { async getUserData(fields) { const params = new URLSearchParams({ fields: Array.isArray(fields) ? fields.join(',') : fields, access_token: this.accessToken }); const url = `${ENDPOINTS.accountEndPoint}/${this.version}/me?${params.toString()}`; const response = await requestHelper({ url, method: 'GET' }); return response; } constructor(accessToken, version = 'v23.0'){ this.accessToken = accessToken; this.version = version; } } exports.InstagramAccount = InstagramAccount; exports.InstagramPublish = InstagramPublish; exports.OAuthInstagram = OAuthInstagram;