Parcourir la source

fix: compile before publish

Acathur il y a 4 ans
Parent
commit
48ec50cd91

+ 0 - 1
.gitignore

@@ -1,6 +1,5 @@
 .DS_Store
 node_modules
-/dist
 
 # Log files
 npm-debug.log*

+ 4 - 0
dist/bridge/constant.d.ts

@@ -0,0 +1,4 @@
+export declare const COOKIE_ROOT_DOMAIN = "proginn.com";
+export declare const COOKIE_APP_KEY = "x_app";
+export declare const COOKIE_ACCESS_TOKEN_KEY = "x_access_token";
+export declare const MSG_REQUIRE_LOGIN = "\u8BF7\u5728\u767B\u5F55\u540E\u64CD\u4F5C";

+ 4 - 0
dist/bridge/constant.js

@@ -0,0 +1,4 @@
+export const COOKIE_ROOT_DOMAIN = 'proginn.com';
+export const COOKIE_APP_KEY = 'x_app';
+export const COOKIE_ACCESS_TOKEN_KEY = 'x_access_token';
+export const MSG_REQUIRE_LOGIN = '请在登录后操作';

+ 31 - 0
dist/bridge/index.d.ts

@@ -0,0 +1,31 @@
+import { Notifier } from '../types/global';
+declare class ProginnBridge {
+    root: any;
+    isAndroid: boolean;
+    private notifier?;
+    constructor(opts?: {
+        notifier?: Notifier;
+    });
+    get cookie(): {
+        [key: string]: string;
+    };
+    get isInApp(): boolean;
+    get isLogined(): boolean;
+    get uid(): string | null;
+    inject(name: string, cb: () => void, root?: string): void;
+    syncCookies(): void;
+    invoke(fn: string, payload?: any): any;
+    back(): void;
+    load(url: string): void;
+    login(): void;
+    checkLogin(force?: boolean): boolean;
+    userLoad(userInfo: any): void;
+    topicLoad(id: string, data: {
+        topic_id: string;
+        user_id: string;
+        share_content: any;
+        topics: any[];
+    }): void;
+    setNavigationBarTitle(title: string): void;
+}
+export default ProginnBridge;

+ 108 - 0
dist/bridge/index.js

@@ -0,0 +1,108 @@
+import cookies from 'js-cookie';
+import { COOKIE_ROOT_DOMAIN, COOKIE_APP_KEY, COOKIE_ACCESS_TOKEN_KEY, MSG_REQUIRE_LOGIN } from './constant';
+const parseJWT = (token) => {
+    const payloadStr = token.split('.')[1];
+    if (!payloadStr) {
+        return null;
+    }
+    try {
+        return JSON.parse(atob(payloadStr));
+    }
+    catch (e) {
+        // tslint:disable-next-line
+        console.error('[parseJWT]', e);
+    }
+    return null;
+};
+class ProginnBridge {
+    constructor(opts) {
+        // @ts-ignore
+        this.root = window.app_event;
+        this.isAndroid = /Android/.test(window.navigator.userAgent);
+        const { notifier } = opts || {};
+        this.notifier = notifier;
+    }
+    get cookie() {
+        return cookies.get();
+    }
+    get isInApp() {
+        // @ts-ignore
+        return !!(cookies.get(COOKIE_APP_KEY) || this.root || window.appBridge);
+    }
+    get isLogined() {
+        return !!cookies.get(COOKIE_ACCESS_TOKEN_KEY);
+    }
+    get uid() {
+        const token = cookies.get(COOKIE_ACCESS_TOKEN_KEY);
+        const payload = token && parseJWT(token);
+        return payload === null || payload === void 0 ? void 0 : payload.uid;
+    }
+    inject(name, cb, root = 'Proginn') {
+        window[root] = window[root] || {};
+        window[root][name] = cb;
+    }
+    syncCookies() {
+        const app = cookies.get(COOKIE_APP_KEY);
+        const token = cookies.get(COOKIE_ACCESS_TOKEN_KEY);
+        const opts = {
+            domain: COOKIE_ROOT_DOMAIN,
+            expires: 7200
+        };
+        if (app) {
+            cookies.set(COOKIE_APP_KEY, app, opts);
+        }
+        if (token) {
+            cookies.set(COOKIE_ACCESS_TOKEN_KEY, token, opts);
+        }
+    }
+    invoke(fn, payload) {
+        if (!this.root) {
+            // tslint:disable-next-line
+            console.warn(`Bridge invoke ${fn} skipped.`);
+            return;
+        }
+        if (this.isAndroid) {
+            return this.root[fn] && this.root[fn](payload);
+        }
+        else {
+            return this.root(fn, payload);
+        }
+    }
+    back() {
+        if (!this.isInApp) {
+            window.history.back();
+        }
+        else {
+            this.invoke('back_page');
+        }
+    }
+    load(url) {
+        window.location.href = url;
+    }
+    login() {
+        if (!this.isInApp) {
+            return;
+        }
+        this.load('proginn://login?backToPage=true');
+    }
+    checkLogin(force = false) {
+        if (force || !this.isLogined) {
+            this.notifier && this.notifier(MSG_REQUIRE_LOGIN);
+            this.login();
+            return false;
+        }
+        return true;
+    }
+    userLoad(userInfo) {
+        this.invoke('user_load', this.isAndroid ? userInfo : {
+            userInfo
+        });
+    }
+    topicLoad(id, data) {
+        this.invoke('topic_load', this.isAndroid ? id : data);
+    }
+    setNavigationBarTitle(title) {
+        this.invoke('setNavigationBarTitle', title);
+    }
+}
+export default ProginnBridge;

+ 4 - 0
dist/index.d.ts

@@ -0,0 +1,4 @@
+export { default as ProginnBridge } from './bridge/index';
+export { default as ProginnRequest } from './request/index';
+export { convertDate } from './utils/converter';
+export { formatTime } from './utils/formatter';

+ 4 - 0
dist/index.js

@@ -0,0 +1,4 @@
+export { default as ProginnBridge } from './bridge/index';
+export { default as ProginnRequest } from './request/index';
+export { convertDate } from './utils/converter';
+export { formatTime } from './utils/formatter';

+ 18 - 0
dist/request/index.d.ts

@@ -0,0 +1,18 @@
+import { AxiosResponse } from 'axios';
+import ProginnBridge from '../bridge';
+import { Notifier } from '../types/global';
+declare const ProginnRequest: {
+    create: (opts: {
+        baseURL: string;
+        bridge?: ProginnBridge;
+        notifier?: Notifier;
+    }) => (opts: {
+        method: 'GET' | 'POST';
+        url: string;
+        query?: any;
+        data?: any;
+        dataType?: 'json' | 'form';
+        headers?: any;
+    }) => Promise<AxiosResponse>;
+};
+export default ProginnRequest;

+ 55 - 0
dist/request/index.js

@@ -0,0 +1,55 @@
+var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
+    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
+    return new (P || (P = Promise))(function (resolve, reject) {
+        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
+        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
+        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
+        step((generator = generator.apply(thisArg, _arguments || [])).next());
+    });
+};
+import Axios from 'axios';
+import qs from 'querystring';
+const factory = (opts) => {
+    const { baseURL, notifier, bridge } = opts || {};
+    const axios = Axios.create({
+        baseURL,
+        withCredentials: true
+    });
+    return (opts) => __awaiter(void 0, void 0, void 0, function* () {
+        const { url, method, query, data, dataType = 'form', headers = {} } = opts;
+        let contentType;
+        if (dataType === 'form') {
+            contentType = 'application/x-www-form-urlencoded';
+        }
+        else if (dataType === 'json') {
+            contentType = 'application/json';
+        }
+        if (method === 'POST') {
+            Object.assign(headers, {
+                'Content-Type': contentType
+            });
+        }
+        const res = yield axios({
+            url,
+            method,
+            params: query,
+            headers,
+            data: data && (dataType === 'form' ? qs.stringify(data) : JSON.stringify(data))
+        });
+        if (res.data) {
+            // require login
+            if (res.data.status === -99) {
+                bridge && bridge.checkLogin(true);
+            }
+            else if (res.data.status !== 1 && res.data.status !== 200) {
+                const message = res.data.data && res.data.data.message || res.data.message || res.data.info;
+                message && notifier && notifier(message);
+            }
+        }
+        return res;
+    });
+};
+const ProginnRequest = {
+    create: factory
+};
+export default ProginnRequest;

+ 1 - 0
dist/types/global.d.ts

@@ -0,0 +1 @@
+export declare type Notifier = (msg: string) => void;

+ 0 - 0
dist/types/global.js


+ 1 - 0
dist/utils/converter.d.ts

@@ -0,0 +1 @@
+export declare const convertDate: (t: number | string) => Date;

+ 9 - 0
dist/utils/converter.js

@@ -0,0 +1,9 @@
+export const convertDate = (t) => {
+    if (typeof t === 'number') {
+        t = String(t).length === 13 ? t : t * 1000;
+    }
+    else if (typeof t === 'string') {
+        t = t.replace(/\s+/g, 'T') + '+08:00';
+    }
+    return new Date(t);
+};

+ 1 - 0
dist/utils/formatter.d.ts

@@ -0,0 +1 @@
+export declare const formatTime: (t: Date | number | string, format: string) => string | null;

+ 26 - 0
dist/utils/formatter.js

@@ -0,0 +1,26 @@
+export const formatTime = (t, format) => {
+    if (typeof t === 'number' || typeof t === 'string') {
+        t = new Date(t);
+    }
+    if (!(t instanceof Date)) {
+        return null;
+    }
+    const o = {
+        'M+': t.getMonth() + 1,
+        'd+': t.getDate(),
+        'h+': t.getHours(),
+        'm+': t.getMinutes(),
+        's+': t.getSeconds(),
+        'q+': Math.floor((t.getMonth() + 3) / 3),
+        'S': t.getMilliseconds() // millisecond
+    };
+    if (/(y+)/.test(format)) {
+        format = format.replace(RegExp.$1, (t.getFullYear() + '').substr(4 - RegExp.$1.length));
+    }
+    for (const k in o) {
+        if (new RegExp('(' + k + ')').test(format)) {
+            format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
+        }
+    }
+    return format;
+};

+ 2 - 3
package.json

@@ -1,14 +1,13 @@
 {
   "name": "proginn-lib",
-  "version": "0.1.1",
+  "version": "0.2.0",
   "description": "Proginn front-end common library.",
   "main": "dist/index.js",
   "module": "dist/index.js",
   "author": "Acathur",
   "private": true,
   "scripts": {
-    "build": "rm -rf dist && tsc",
-    "install": "tsc"
+    "build": "rm -rf dist && tsc"
   },
   "files": [
     "dist/**/*"