All files / src/apis/v2 index.ts

100% Statements 19/19
100% Branches 6/6
100% Functions 2/2
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92                                  109x 109x       45x 45x                 45x   45x 3x 1x         2x             45x     109x 109x 109x 109x 109x 109x 109x   109x 109x                                                              
import { DelimiterType, rfetch, RibbonFetchBasicOptions } from '@ribbon-studios/js-utils';
import type { GuildWars2, SupportedLanguages } from '@/index';
import { Schema } from '@/types/v2';
import { bind } from '@/utils';
 
import * as account from './account';
import * as achievements from './achievements';
import * as items from './items';
import * as minis from './minis';
import * as races from './races';
import * as skins from './skins';
import * as titles from './titles';
 
import { build } from './build';
import { tokeninfo } from './tokeninfo';
 
export class V2<V extends Schema> implements V2.API<V> {
  constructor(public config: GuildWars2.InternalConfig<V>) {
    rfetch.delimiters(DelimiterType.COMMA);
  }
 
  async fetch<T>(endpoint: string, options?: V2.FetchOptions): Promise<T> {
    const url = new URL(endpoint, 'https://api.guildwars2.com');
    const rfetchOptions: RibbonFetchBasicOptions = {
      ...options,
      params: {
        v: this.config.v,
        ...options?.params,
      },
    };
 
    const access_token =
      typeof options?.params?.access_token === 'string' ? options?.params?.access_token : this.config.access_token;
 
    if (access_token) {
      if (typeof process === 'object') {
        rfetchOptions.headers = {
          Authorization: `Bearer ${access_token}`,
          ...rfetchOptions.headers,
        };
      } else {
        rfetchOptions.params = {
          access_token,
          ...rfetchOptions.params,
        };
      }
    }
 
    return rfetch.get<T>(url.toString(), rfetchOptions);
  }
 
  account = bind(account, this as V2.API<V>);
  achievements = bind(achievements, this as V2.API<V>);
  items = bind(items, this as V2.API<V>);
  minis = bind(minis, this as V2.API<V>);
  races = bind(races, this as V2.API<V>);
  skins = bind(skins, this as V2.API<V>);
  titles = bind(titles, this as V2.API<V>);
 
  build = build;
  tokeninfo = tokeninfo;
}
 
export namespace V2 {
  export interface API<V extends Schema> {
    config: GuildWars2.InternalConfig<V>;
 
    fetch<T>(endpoint: string, options?: V2.FetchOptions): Promise<T>;
  }
 
  export type FetchOptions = Pick<RibbonFetchBasicOptions, 'params' | 'headers'>;
 
  type SchemaOptions<V extends Schema> = {
    /**
     * The schema version.
     */
    v?: V;
  };
 
  type SchemaOptionsWithLang<V extends Schema> = SchemaOptions<V> & {
    /**
     * Show localized texts in the specified language.
     * @default 'en'
     */
    lang?: SupportedLanguages;
  };
 
  export type Options<V extends Schema, WithLang extends boolean = false> = WithLang extends true
    ? SchemaOptionsWithLang<V>
    : SchemaOptions<V>;
}