@gw2me/client 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 GW2Treasures
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # `@gw2me/client`
2
+
3
+ This is a client library to interact with the gw2.me API.
4
+
5
+ ## Usage
6
+
7
+ ```ts
8
+ import { Gw2MeClient } from '@gw2me/client';
9
+
10
+ const client = new Gw2MeClient({ client_id: '<client_id>' });
11
+ const url = client.getAuthorizationUrl({ /* ... */ });
12
+
13
+ // redirect user to url
14
+ ```
15
+
16
+ ## Installation
17
+
18
+ ```
19
+ npm i @gw2me/client
20
+ ```
21
+
22
+ ## License
23
+
24
+ **@gw2me/client** is licensed under the [MIT License](./LICENSE).
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare enum Scope {
1
+ declare enum Scope {
2
2
  Identify = "identify",
3
3
  Email = "email",
4
4
  GW2_Account = "gw2:account",
@@ -12,63 +12,75 @@ export declare enum Scope {
12
12
  GW2_Progression = "gw2:progression",
13
13
  GW2_Guilds = "gw2:guilds"
14
14
  }
15
- export interface AuthorizationUrlParams {
16
- redirect_uri: string;
15
+ interface ClientInfo {
17
16
  client_id: string;
17
+ client_secret?: string;
18
+ }
19
+ interface Options {
20
+ url: string;
21
+ }
22
+
23
+ interface UserResponse {
24
+ user: {
25
+ id: string;
26
+ name: string;
27
+ email?: string;
28
+ };
29
+ }
30
+ interface AccountsResponse {
31
+ accounts: {
32
+ id: string;
33
+ name: string;
34
+ }[];
35
+ }
36
+ interface SubtokenResponse {
37
+ subtoken: string;
38
+ expiresAt: string;
39
+ }
40
+ declare class Gw2MeApi {
41
+ #private;
42
+ private access_token;
43
+ private options?;
44
+ constructor(access_token: string, options?: Partial<Options> | undefined);
45
+ user(): Promise<UserResponse>;
46
+ accounts(): Promise<AccountsResponse>;
47
+ subtoken(accountId: string): Promise<SubtokenResponse>;
48
+ }
49
+
50
+ interface AuthorizationUrlParams {
51
+ redirect_uri: string;
18
52
  scopes: Scope[];
19
53
  state?: string;
20
54
  code_challenge?: string;
21
55
  code_challenge_method?: 'S256';
56
+ prompt?: 'none' | 'consent';
57
+ include_granted_scopes?: boolean;
22
58
  }
23
- export declare function getAuthorizationUrl({ redirect_uri, client_id, scopes, state, code_challenge, code_challenge_method }: AuthorizationUrlParams): string;
24
- export interface AuthTokenParams {
59
+ interface AuthTokenParams {
25
60
  code: string;
26
61
  redirect_uri: string;
27
- client_id: string;
28
- client_secret?: string;
29
62
  code_verifier?: string;
30
63
  }
31
- export interface RefreshTokenParams {
64
+ interface RefreshTokenParams {
32
65
  refresh_token: string;
33
- client_id: string;
34
- client_secret: string;
35
66
  }
36
- export interface TokenResponse {
67
+ interface TokenResponse {
37
68
  access_token: string;
38
69
  token_type: 'Bearer';
39
70
  expires_in: number;
40
71
  refresh_token?: string;
41
72
  scope: string;
42
73
  }
43
- export declare function getAccessToken({ code, client_id, client_secret, redirect_uri, code_verifier }: AuthTokenParams): Promise<TokenResponse>;
44
- export declare function refreshToken({ refresh_token, client_id, client_secret }: RefreshTokenParams): Promise<TokenResponse>;
45
- export interface UserResponse {
46
- user: {
47
- id: string;
48
- name: string;
49
- email?: string;
50
- };
51
- }
52
- export interface AccountsResponse {
53
- accounts: {
54
- id: string;
55
- name: string;
56
- }[];
57
- }
58
- export interface SubtokenResponse {
59
- subtoken: string;
60
- expiresAt: string;
74
+ declare class Gw2MeClient {
75
+ #private;
76
+ private options?;
77
+ private client_id;
78
+ private client_secret?;
79
+ constructor({ client_id, client_secret }: ClientInfo, options?: Partial<Options> | undefined);
80
+ getAuthorizationUrl({ redirect_uri, scopes, state, code_challenge, code_challenge_method, prompt, include_granted_scopes }: AuthorizationUrlParams): string;
81
+ getAccessToken({ code, redirect_uri, code_verifier }: AuthTokenParams): Promise<TokenResponse>;
82
+ refreshToken({ refresh_token }: RefreshTokenParams): Promise<TokenResponse>;
83
+ api(access_token: string): Gw2MeApi;
61
84
  }
62
- export declare const rest: {
63
- user({ access_token }: {
64
- access_token: string;
65
- }): Promise<UserResponse>;
66
- accounts({ access_token }: {
67
- access_token: string;
68
- }): Promise<AccountsResponse>;
69
- subtoken({ access_token, accountId }: {
70
- access_token: string;
71
- accountId: string;
72
- }): Promise<SubtokenResponse>;
73
- };
74
- //# sourceMappingURL=index.d.ts.map
85
+
86
+ export { AccountsResponse, AuthTokenParams, AuthorizationUrlParams, ClientInfo, Gw2MeApi, Gw2MeClient, Options, RefreshTokenParams, Scope, SubtokenResponse, TokenResponse, UserResponse };
package/dist/index.js CHANGED
@@ -1,151 +1 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var __async = (__this, __arguments, generator) => {
20
- return new Promise((resolve, reject) => {
21
- var fulfilled = (value) => {
22
- try {
23
- step(generator.next(value));
24
- } catch (e) {
25
- reject(e);
26
- }
27
- };
28
- var rejected = (value) => {
29
- try {
30
- step(generator.throw(value));
31
- } catch (e) {
32
- reject(e);
33
- }
34
- };
35
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
36
- step((generator = generator.apply(__this, __arguments)).next());
37
- });
38
- };
39
-
40
- // src/index.ts
41
- var src_exports = {};
42
- __export(src_exports, {
43
- Scope: () => Scope,
44
- getAccessToken: () => getAccessToken,
45
- getAuthorizationUrl: () => getAuthorizationUrl,
46
- refreshToken: () => refreshToken,
47
- rest: () => rest
48
- });
49
- module.exports = __toCommonJS(src_exports);
50
- var Scope = /* @__PURE__ */ ((Scope2) => {
51
- Scope2["Identify"] = "identify";
52
- Scope2["Email"] = "email";
53
- Scope2["GW2_Account"] = "gw2:account";
54
- Scope2["GW2_Inventories"] = "gw2:inventories";
55
- Scope2["GW2_Characters"] = "gw2:characters";
56
- Scope2["GW2_Tradingpost"] = "gw2:tradingpost";
57
- Scope2["GW2_Wallet"] = "gw2:wallet";
58
- Scope2["GW2_Unlocks"] = "gw2:unlocks";
59
- Scope2["GW2_Pvp"] = "gw2:pvp";
60
- Scope2["GW2_Builds"] = "gw2:builds";
61
- Scope2["GW2_Progression"] = "gw2:progression";
62
- Scope2["GW2_Guilds"] = "gw2:guilds";
63
- return Scope2;
64
- })(Scope || {});
65
- function getUrl() {
66
- return process.env.GW2ME_URL || "https://gw2.me/";
67
- }
68
- function getAuthorizationUrl({ redirect_uri, client_id, scopes, state, code_challenge, code_challenge_method }) {
69
- const params = new URLSearchParams({
70
- "response_type": "code",
71
- "redirect_uri": redirect_uri,
72
- "client_id": client_id,
73
- "scope": scopes.join(" ")
74
- });
75
- if (state) {
76
- params.append("state", state);
77
- }
78
- if (code_challenge && code_challenge_method) {
79
- params.append("code_challenge", code_challenge);
80
- params.append("code_challenge_method", code_challenge_method);
81
- }
82
- return `${getUrl()}oauth2/authorize?${params.toString()}`;
83
- }
84
- function getAccessToken(_0) {
85
- return __async(this, arguments, function* ({ code, client_id, client_secret, redirect_uri, code_verifier }) {
86
- const data = new URLSearchParams({
87
- grant_type: "authorization_code",
88
- code,
89
- client_id,
90
- redirect_uri
91
- });
92
- if (client_secret) {
93
- data.set("client_secret", client_secret);
94
- }
95
- if (code_verifier) {
96
- data.set("code_verifier", code_verifier);
97
- }
98
- const token = yield fetch(`${getUrl()}api/token`, {
99
- method: "POST",
100
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
101
- body: data,
102
- cache: "no-store"
103
- }).then((r) => r.json());
104
- return token;
105
- });
106
- }
107
- function refreshToken(_0) {
108
- return __async(this, arguments, function* ({ refresh_token, client_id, client_secret }) {
109
- const data = new URLSearchParams({
110
- grant_type: "refresh_token",
111
- refresh_token,
112
- client_id,
113
- client_secret
114
- });
115
- const token = yield fetch(`${getUrl()}api/token`, {
116
- method: "POST",
117
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
118
- body: data,
119
- cache: "no-store"
120
- }).then((r) => r.json());
121
- return token;
122
- });
123
- }
124
- var rest = {
125
- user({ access_token }) {
126
- return fetch(`${getUrl()}api/user`, {
127
- headers: { "Authorization": `Bearer ${access_token}` },
128
- cache: "no-store"
129
- }).then((r) => r.json());
130
- },
131
- accounts({ access_token }) {
132
- return fetch(`${getUrl()}api/accounts`, {
133
- headers: { "Authorization": `Bearer ${access_token}` },
134
- cache: "no-store"
135
- }).then((r) => r.json());
136
- },
137
- subtoken({ access_token, accountId }) {
138
- return fetch(`${getUrl()}api/accounts/${accountId}/subtoken`, {
139
- headers: { "Authorization": `Bearer ${access_token}` },
140
- cache: "no-store"
141
- }).then((r) => r.json());
142
- }
143
- };
144
- // Annotate the CommonJS export names for ESM import in node:
145
- 0 && (module.exports = {
146
- Scope,
147
- getAccessToken,
148
- getAuthorizationUrl,
149
- refreshToken,
150
- rest
151
- });
1
+ "use strict";var k=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var y=(n,e)=>{for(var t in e)k(n,t,{get:e[t],enumerable:!0})},b=(n,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of T(e))!A.call(n,i)&&i!==t&&k(n,i,{get:()=>e[i],enumerable:!(s=R(e,i))||s.enumerable});return n};var U=n=>b(k({},"__esModule",{value:!0}),n);var W=(n,e,t)=>{if(!e.has(n))throw TypeError("Cannot "+t)};var g=(n,e,t)=>{if(e.has(n))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(n):e.set(n,t)};var c=(n,e,t)=>(W(n,e,"access private method"),t);var w=(n,e,t)=>new Promise((s,i)=>{var a=r=>{try{p(t.next(r))}catch(m){i(m)}},u=r=>{try{p(t.throw(r))}catch(m){i(m)}},p=r=>r.done?s(r.value):Promise.resolve(r.value).then(a,u);p((t=t.apply(n,e)).next())});var $={};y($,{Gw2MeApi:()=>d,Gw2MeClient:()=>P,Scope:()=>x});module.exports=U($);var x=(o=>(o.Identify="identify",o.Email="email",o.GW2_Account="gw2:account",o.GW2_Inventories="gw2:inventories",o.GW2_Characters="gw2:characters",o.GW2_Tradingpost="gw2:tradingpost",o.GW2_Wallet="gw2:wallet",o.GW2_Unlocks="gw2:unlocks",o.GW2_Pvp="gw2:pvp",o.GW2_Builds="gw2:builds",o.GW2_Progression="gw2:progression",o.GW2_Guilds="gw2:guilds",o))(x||{});var h,_,d=class{constructor(e,t){this.access_token=e;this.options=t;g(this,h)}user(){return fetch(`${c(this,h,_).call(this)}api/user`,{headers:{Authorization:`Bearer ${this.access_token}`},cache:"no-store"}).then(e=>e.json())}accounts(){return fetch(`${c(this,h,_).call(this)}api/accounts`,{headers:{Authorization:`Bearer ${this.access_token}`},cache:"no-store"}).then(e=>e.json())}subtoken(e){return fetch(`${c(this,h,_).call(this)}api/accounts/${e}/subtoken`,{headers:{Authorization:`Bearer ${this.access_token}`},cache:"no-store"}).then(t=>t.json())}};h=new WeakSet,_=function(){var e;return((e=this.options)==null?void 0:e.url)||"https://gw2.me/"};var l,f,P=class{constructor({client_id:e,client_secret:t},s){this.options=s;g(this,l);this.client_id=e,this.client_secret=t}getAuthorizationUrl({redirect_uri:e,scopes:t,state:s,code_challenge:i,code_challenge_method:a,prompt:u,include_granted_scopes:p}){let r=new URLSearchParams({client_id:this.client_id,response_type:"code",redirect_uri:e,scope:t.join(" ")});return s&&r.append("state",s),i&&a&&(r.append("code_challenge",i),r.append("code_challenge_method",a)),u&&r.append("prompt",u),p&&r.append("include_granted_scopes","true"),`${c(this,l,f).call(this)}oauth2/authorize?${r.toString()}`}getAccessToken(i){return w(this,arguments,function*({code:e,redirect_uri:t,code_verifier:s}){let a=new URLSearchParams({grant_type:"authorization_code",code:e,client_id:this.client_id,redirect_uri:t});return this.client_secret&&a.set("client_secret",this.client_secret),s&&a.set("code_verifier",s),yield fetch(`${c(this,l,f).call(this)}api/token`,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:a,cache:"no-store"}).then(p=>p.json())})}refreshToken(t){return w(this,arguments,function*({refresh_token:e}){if(!this.client_secret)throw new Error("client_secret required");let s=new URLSearchParams({grant_type:"refresh_token",refresh_token:e,client_id:this.client_id,client_secret:this.client_secret});return yield fetch(`${c(this,l,f).call(this)}api/token`,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:s,cache:"no-store"}).then(a=>a.json())})}api(e){return new d(e,this.options)}};l=new WeakSet,f=function(){var e;return((e=this.options)==null?void 0:e.url)||"https://gw2.me/"};0&&(module.exports={Gw2MeApi,Gw2MeClient,Scope});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gw2me/client",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "gw2.me client library",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -10,8 +10,7 @@
10
10
  "build:ci": "tsup src/index.ts --minify --dts",
11
11
  "clean": "rm -rf dist/",
12
12
  "lint": "eslint src",
13
- "publish-package": "if test `npm view . version | sed -n 2p` != $npm_package_version; then npm publish --access public; else echo not changed; fi",
14
- "test": "echo \"Error: no test specified\" && exit 1"
13
+ "publish-package": "if test `npm view . version | sed -n 2p` != $npm_package_version; then npm publish --access public; else echo not changed; fi"
15
14
  },
16
15
  "repository": {
17
16
  "type": "git",
@@ -35,11 +34,11 @@
35
34
  "homepage": "https://github.com/gw2treasures/gw2.me#readme",
36
35
  "devDependencies": {
37
36
  "@tsconfig/recommended": "1.0.3",
38
- "@typescript-eslint/eslint-plugin": "6.7.4",
39
- "@typescript-eslint/parser": "6.7.4",
40
- "eslint": "8.50.0",
37
+ "@typescript-eslint/eslint-plugin": "6.7.5",
38
+ "@typescript-eslint/parser": "6.7.5",
39
+ "eslint": "8.51.0",
41
40
  "tsup": "7.2.0",
42
41
  "typescript": "5.2.2",
43
- "undici-types": "5.25.3"
42
+ "undici-types": "5.26.3"
44
43
  }
45
44
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,oBAAY,KAAK;IACf,QAAQ,aAAa;IACrB,KAAK,UAAU;IAEf,WAAW,gBAAgB;IAC3B,eAAe,oBAAoB;IACnC,cAAc,mBAAmB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