main.js

import { each, isString } from './util'
import { VERSION, KakaoError, getAppKey, setAppKey } from './common'

import Auth from './Auth'
import API from './API'
import Link from './Link'
import Channel from './Channel'
import PlusFriend from './PlusFriend'
import Story from './Story'
import Navi from './Navi'

/**
 * 공통적으로 사용하는 함수들이 포함되어 있습니다.
 * @namespace Kakao
 */

/**
 * 카카오 JavaScript SDK의 버전을 반환합니다.
 * @constant
 * @type {String}
 * @memberof Kakao
 */
export {
  VERSION
}

// 명확하게 lint-ignore 표시 & 주석
if (__CHANNEL_ONLY__) {
  exports.Channel = Channel
  exports.PlusFriend = PlusFriend
}

if (__STORY_ONLY__) {
  exports.Story = Story
}

/**
 * 카카오 JavaScript SDK를 초기화합니다. SDK를 사용하기 전에 호출해야 합니다.
 * @function init
 * @param {String} appKey JavaScript 키
 * @throws {KakaoError} 앱키가 유효하지 않을 때 에러가 발생합니다.
 * @memberof Kakao
 */
export function init(appKey) {
  if (isInitialized()) {
    throw new KakaoError('Kakao.init: Already initialized')
  }

  if (!isString(appKey)) {
    throw new KakaoError('Kakao.init: App key must be provided')
  }

  setAppKey(appKey)

  if (!__CHANNEL_ONLY__ && !__STORY_ONLY__) {
    this.Auth = Auth
    this.API = API
    this.Link = Link
    this.Channel = Channel
    this.PlusFriend = PlusFriend
    this.Story = Story
    this.Navi = Navi
  }
}

/**
 * 카카오 JavaScript SDK의 초기화 여부를 반환합니다.
 * @function isInitialized
 * @returns {boolean} 초기화되었다면 true, 초기화되지 않았다면 false.
 * @memberof Kakao
 */
export function isInitialized() {
  return getAppKey() !== null
}

/**
 * Kakao JavaScript SDK에서 사용한 리소스를 해제합니다.
 * @function cleanup
 * @memberof Kakao
 */
export function cleanup() {
  each([
    'Auth',
    'API',
    'Link',
    'Channel',
    'PlusFriend',
    'Story',
    'Navi',
  ], e => this[e] && this[e].cleanup())

  setAppKey(null)
}