UNPKG

@candriajs/git-neko-kit

Version:

Github, Gitee, GitCode API封装库

1 lines 3.01 kB
import{AccessTokenSuccessMsg as e,AccessTokenValidMsg as t,ExpiredAccessTokenMsg as n,FailedToFetchAccessTokenMsg as r,FailedToRefreshAccessTokenMsg as i,GitHubClient as a,InvalidAccessTokenMsg as o,MissingAccessCodeMsg as s,MissingAccessTokenMsg as c,MissingAppClientMsg as l,MissingRefreshTokenMsg as u,PermissionDeniedMsg as d,RefreshAccessTokenSuccessMsg as f}from"./client-sGV7POG-.mjs";var p=class extends a{constructor(e){super(e),this.userToken=e.userToken,this.api_url=e.api_url,this.base_url=e.base_url}async get_token_by_code(t){if(!this.is_app_client)throw Error(l);if(!t.code)throw Error(s);try{this.setRequestConfig({url:this.base_url});let n=await this.post(`/login/oauth/access_token`,{client_id:this.Client_ID,client_secret:this.Client_Secret,code:t.code},{Accept:`application/json`}),i=n.status===`ok`&&n.statusCode===200&&!n.data.error;if(!i)throw Error(r);if(n.data){let t={success:i,message:e,access_token:n.data.access_token,expires_in:n.data.expires_in??null,refresh_token:n.data.refresh_token??null,refresh_token_expires_in:n.data.refresh_token_expires_in??null,scope:n.data.scope,token_type:n.data.token_type};n.data=t}return n}catch(e){throw Error(`[GitHub] 获取访问令牌失败: ${e.message}`)}}async check_token_status(e){try{if(!this.is_app_client)throw Error(l);let r=e?.access_token??this.userToken;if(!r)throw Error(o);if(!r.startsWith(`ghu_`))throw Error(c);this.setRequestConfig({url:this.api_url,tokenType:`Basic`,token:`${this.Client_ID}:${this.Client_Secret}`});let i=await this.post(`/applications/${this.Client_ID}/token`,{access_token:r}),a=!(i.status===`ok`&&(i.statusCode===404||i.statusCode===422)),s={success:a,message:a?t:n};return i.data=s,i}catch(e){throw Error(`[GitHub] 获取访问令牌状态失败: ${e.message}`)}}async refresh_token(e){try{if(!this.is_app_client)throw Error(l);if(!e.refresh_token)throw Error(s);if(!e.refresh_token.startsWith(`ghr_`))throw Error(u);this.setRequestConfig({url:this.base_url});let t=await this.post(`/login/oauth/access_token`,{client_id:this.Client_ID,client_secret:this.Client_Secret,grant_type:`refresh_token`,refresh_token:e.refresh_token},{Accept:`application/json`}),n=t.status===`ok`&&t.statusCode===200&&!t.data.error,r=i;switch(t.data.error){case`bad_refresh_token`:r=u;break;case`unauthorized`:r=d;break}if(!n)throw Error(r);if(t.data){let e={success:n,message:f,access_token:t.data.access_token,expires_in:t.data.expires_in??null,refresh_token:t.data.refresh_token??null,refresh_token_expires_in:t.data.refresh_token_expires_in??null,scope:t.data.scope,token_type:t.data.token_type};t.data=e}return t}catch(e){throw Error(`[GitHub] 刷新访问令牌失败: ${e.message}`)}}async create_auth_link(e){try{if(!this.is_app_client)throw Error(l);let t=new URL(`/login/oauth/authorize`,this.base_url);return t.search=new URLSearchParams({client_id:this.Client_ID,...e&&{state:e}}).toString(),Promise.resolve(t.toString())}catch(e){throw Error(`[GitHub] 生成授权链接失败: ${e.message}`)}}};export{p as Auth};