Isolate Netlify, Firebase and Helper functions + Import from absolute paths
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
// Docs on event and context https://www.netlify.com/docs/functions/#the-handler-method
|
||||
exports.handler = async (event, context) => {
|
||||
switch (event.httpMethod) {
|
||||
case "GET":
|
||||
try {
|
||||
const name = event.queryStringParameters.name || "World"
|
||||
return {
|
||||
statusCode: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ message: `Hello ${name}` }),
|
||||
}
|
||||
} catch (err) {
|
||||
return { statusCode: 500, body: err.toString() }
|
||||
}
|
||||
|
||||
default:
|
||||
return { statusCode: 405, body: "Method Not Allowed" }
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
const mimeToMode = {
|
||||
"text/plain": "plain_text",
|
||||
"text/html": "html",
|
||||
"application/xml": "xml",
|
||||
"application/hal+json": "json",
|
||||
"application/json": "json",
|
||||
}
|
||||
|
||||
export function getEditorLangForMimeType(mimeType) {
|
||||
return mimeToMode[mimeType] || "plain_text"
|
||||
}
|
||||
225
functions/fb.js
225
functions/fb.js
@@ -1,225 +0,0 @@
|
||||
import firebase from "firebase/app"
|
||||
import "firebase/firestore"
|
||||
import "firebase/auth"
|
||||
|
||||
// Initialize Firebase, copied from cloud console
|
||||
const firebaseConfig = {
|
||||
apiKey: process.env.API_KEY || "AIzaSyCMsFreESs58-hRxTtiqQrIcimh4i1wbsM",
|
||||
authDomain: process.env.AUTH_DOMAIN || "postwoman-api.firebaseapp.com",
|
||||
databaseURL: process.env.DATABASE_URL || "https://postwoman-api.firebaseio.com",
|
||||
projectId: process.env.PROJECT_ID || "postwoman-api",
|
||||
storageBucket: process.env.STORAGE_BUCKET || "postwoman-api.appspot.com",
|
||||
messagingSenderId: process.env.MESSAGING_SENDER_ID || "421993993223",
|
||||
appId: process.env.APP_ID || "1:421993993223:web:ec0baa8ee8c02ffa1fc6a2",
|
||||
measurementId: process.env.MEASUREMENT_ID || "G-ERJ6025CEB",
|
||||
}
|
||||
firebase.initializeApp(firebaseConfig)
|
||||
|
||||
// a reference to the users collection
|
||||
const usersCollection = firebase.firestore().collection("users")
|
||||
|
||||
// the shared state object that any vue component
|
||||
// can get access to
|
||||
export const fb = {
|
||||
currentUser: null,
|
||||
currentFeeds: [],
|
||||
currentSettings: [],
|
||||
currentHistory: [],
|
||||
currentCollections: [],
|
||||
currentEnvironments: [],
|
||||
writeFeeds: async (message, label) => {
|
||||
const dt = {
|
||||
createdOn: new Date(),
|
||||
author: fb.currentUser.uid,
|
||||
author_name: fb.currentUser.displayName,
|
||||
author_image: fb.currentUser.photoURL,
|
||||
message,
|
||||
label,
|
||||
}
|
||||
usersCollection
|
||||
.doc(fb.currentUser.uid)
|
||||
.collection("feeds")
|
||||
.add(dt)
|
||||
.catch((e) => console.error("error inserting", dt, e))
|
||||
},
|
||||
deleteFeed: (id) => {
|
||||
usersCollection
|
||||
.doc(fb.currentUser.uid)
|
||||
.collection("feeds")
|
||||
.doc(id)
|
||||
.delete()
|
||||
.catch((e) => console.error("error deleting", id, e))
|
||||
},
|
||||
writeSettings: async (setting, value) => {
|
||||
const st = {
|
||||
updatedOn: new Date(),
|
||||
author: fb.currentUser.uid,
|
||||
author_name: fb.currentUser.displayName,
|
||||
author_image: fb.currentUser.photoURL,
|
||||
name: setting,
|
||||
value,
|
||||
}
|
||||
usersCollection
|
||||
.doc(fb.currentUser.uid)
|
||||
.collection("settings")
|
||||
.doc(setting)
|
||||
.set(st)
|
||||
.catch((e) => console.error("error updating", st, e))
|
||||
},
|
||||
writeHistory: async (entry) => {
|
||||
const hs = entry
|
||||
usersCollection
|
||||
.doc(fb.currentUser.uid)
|
||||
.collection("history")
|
||||
.add(hs)
|
||||
.catch((e) => console.error("error inserting", hs, e))
|
||||
},
|
||||
deleteHistory: (entry) => {
|
||||
usersCollection
|
||||
.doc(fb.currentUser.uid)
|
||||
.collection("history")
|
||||
.doc(entry.id)
|
||||
.delete()
|
||||
.catch((e) => console.error("error deleting", entry, e))
|
||||
},
|
||||
clearHistory: () => {
|
||||
usersCollection
|
||||
.doc(fb.currentUser.uid)
|
||||
.collection("history")
|
||||
.get()
|
||||
.then(({ docs }) => {
|
||||
docs.forEach((e) => fb.deleteHistory(e))
|
||||
})
|
||||
},
|
||||
toggleStar: (entry, value) => {
|
||||
usersCollection
|
||||
.doc(fb.currentUser.uid)
|
||||
.collection("history")
|
||||
.doc(entry.id)
|
||||
.update({ star: value })
|
||||
.catch((e) => console.error("error deleting", entry, e))
|
||||
},
|
||||
writeCollections: async (collection) => {
|
||||
const cl = {
|
||||
updatedOn: new Date(),
|
||||
author: fb.currentUser.uid,
|
||||
author_name: fb.currentUser.displayName,
|
||||
author_image: fb.currentUser.photoURL,
|
||||
collection,
|
||||
}
|
||||
usersCollection
|
||||
.doc(fb.currentUser.uid)
|
||||
.collection("collections")
|
||||
.doc("sync")
|
||||
.set(cl)
|
||||
.catch((e) => console.error("error updating", cl, e))
|
||||
},
|
||||
writeEnvironments: async (environment) => {
|
||||
const ev = {
|
||||
updatedOn: new Date(),
|
||||
author: fb.currentUser.uid,
|
||||
author_name: fb.currentUser.displayName,
|
||||
author_image: fb.currentUser.photoURL,
|
||||
environment,
|
||||
}
|
||||
usersCollection
|
||||
.doc(fb.currentUser.uid)
|
||||
.collection("environments")
|
||||
.doc("sync")
|
||||
.set(ev)
|
||||
.catch((e) => console.error("error updating", ev, e))
|
||||
},
|
||||
}
|
||||
|
||||
// When a user logs in or out, save that in the store
|
||||
firebase.auth().onAuthStateChanged((user) => {
|
||||
if (user) {
|
||||
fb.currentUser = user
|
||||
fb.currentUser.providerData.forEach((profile) => {
|
||||
let us = {
|
||||
updatedOn: new Date(),
|
||||
provider: profile.providerId,
|
||||
name: profile.displayName,
|
||||
email: profile.email,
|
||||
photoUrl: profile.photoURL,
|
||||
uid: profile.uid,
|
||||
}
|
||||
usersCollection
|
||||
.doc(fb.currentUser.uid)
|
||||
.set(us)
|
||||
.catch((e) => console.error("error updating", us, e))
|
||||
})
|
||||
|
||||
usersCollection
|
||||
.doc(fb.currentUser.uid)
|
||||
.collection("feeds")
|
||||
.orderBy("createdOn", "desc")
|
||||
.onSnapshot((feedsRef) => {
|
||||
const feeds = []
|
||||
feedsRef.forEach((doc) => {
|
||||
const feed = doc.data()
|
||||
feed.id = doc.id
|
||||
feeds.push(feed)
|
||||
})
|
||||
fb.currentFeeds = feeds
|
||||
})
|
||||
|
||||
usersCollection
|
||||
.doc(fb.currentUser.uid)
|
||||
.collection("settings")
|
||||
.onSnapshot((settingsRef) => {
|
||||
const settings = []
|
||||
settingsRef.forEach((doc) => {
|
||||
const setting = doc.data()
|
||||
setting.id = doc.id
|
||||
settings.push(setting)
|
||||
})
|
||||
fb.currentSettings = settings
|
||||
})
|
||||
|
||||
usersCollection
|
||||
.doc(fb.currentUser.uid)
|
||||
.collection("history")
|
||||
.onSnapshot((historyRef) => {
|
||||
const history = []
|
||||
historyRef.forEach((doc) => {
|
||||
const entry = doc.data()
|
||||
entry.id = doc.id
|
||||
history.push(entry)
|
||||
})
|
||||
fb.currentHistory = history
|
||||
})
|
||||
|
||||
usersCollection
|
||||
.doc(fb.currentUser.uid)
|
||||
.collection("collections")
|
||||
.onSnapshot((collectionsRef) => {
|
||||
const collections = []
|
||||
collectionsRef.forEach((doc) => {
|
||||
const collection = doc.data()
|
||||
collection.id = doc.id
|
||||
collections.push(collection)
|
||||
})
|
||||
if (collections.length > 0) {
|
||||
fb.currentCollections = collections[0].collection
|
||||
}
|
||||
})
|
||||
|
||||
usersCollection
|
||||
.doc(fb.currentUser.uid)
|
||||
.collection("environments")
|
||||
.onSnapshot((environmentsRef) => {
|
||||
const environments = []
|
||||
environmentsRef.forEach((doc) => {
|
||||
const environment = doc.data()
|
||||
environment.id = doc.id
|
||||
environments.push(environment)
|
||||
})
|
||||
if (environments.length > 0) {
|
||||
fb.currentEnvironments = environments[0].environment
|
||||
}
|
||||
})
|
||||
} else {
|
||||
fb.currentUser = null
|
||||
}
|
||||
})
|
||||
@@ -1,124 +0,0 @@
|
||||
export const commonHeaders = [
|
||||
"WWW-Authenticate",
|
||||
"Authorization",
|
||||
"Proxy-Authenticate",
|
||||
"Proxy-Authorization",
|
||||
"Age",
|
||||
"Cache-Control",
|
||||
"Clear-Site-Data",
|
||||
"Expires",
|
||||
"Pragma",
|
||||
"Warning",
|
||||
"Accept-CH",
|
||||
"Accept-CH-Lifetime",
|
||||
"Early-Data",
|
||||
"Content-DPR",
|
||||
"DPR",
|
||||
"Device-Memory",
|
||||
"Save-Data",
|
||||
"Viewport-Width",
|
||||
"Width",
|
||||
"Last-Modified",
|
||||
"ETag",
|
||||
"If-Match",
|
||||
"If-None-Match",
|
||||
"If-Modified-Since",
|
||||
"If-Unmodified-Since",
|
||||
"Vary",
|
||||
"Connection",
|
||||
"Keep-Alive",
|
||||
"Accept",
|
||||
"Accept-Charset",
|
||||
"Accept-Encoding",
|
||||
"Accept-Language",
|
||||
"Expect",
|
||||
"Max-Forwards",
|
||||
"Cookie",
|
||||
"Set-Cookie",
|
||||
"Cookie2",
|
||||
"Set-Cookie2",
|
||||
"Access-Control-Allow-Origin",
|
||||
"Access-Control-Allow-Credentials",
|
||||
"Access-Control-Allow-Headers",
|
||||
"Access-Control-Allow-Methods",
|
||||
"Access-Control-Expose-Headers",
|
||||
"Access-Control-Max-Age",
|
||||
"Access-Control-Request-Headers",
|
||||
"Access-Control-Request-Method",
|
||||
"Origin",
|
||||
"Service-Worker-Allowed",
|
||||
"Timing-Allow-Origin",
|
||||
"X-Permitted-Cross-Domain-Policies",
|
||||
"DNT",
|
||||
"Tk",
|
||||
"Content-Disposition",
|
||||
"Content-Length",
|
||||
"Content-Type",
|
||||
"Content-Encoding",
|
||||
"Content-Language",
|
||||
"Content-Location",
|
||||
"Forwarded",
|
||||
"X-Forwarded-For",
|
||||
"X-Forwarded-Host",
|
||||
"X-Forwarded-Proto",
|
||||
"Via",
|
||||
"Location",
|
||||
"From",
|
||||
"Host",
|
||||
"Referer",
|
||||
"Referrer-Policy",
|
||||
"User-Agent",
|
||||
"Allow",
|
||||
"Server",
|
||||
"Accept-Ranges",
|
||||
"Range",
|
||||
"If-Range",
|
||||
"Content-Range",
|
||||
"Cross-Origin-Opener-Policy",
|
||||
"Cross-Origin-Resource-Policy",
|
||||
"Content-Security-Policy",
|
||||
"Content-Security-Policy-Report-Only",
|
||||
"Expect-CT",
|
||||
"Feature-Policy",
|
||||
"Public-Key-Pins",
|
||||
"Public-Key-Pins-Report-Only",
|
||||
"Strict-Transport-Security",
|
||||
"Upgrade-Insecure-Requests",
|
||||
"X-Content-Type-Options",
|
||||
"X-Download-Options",
|
||||
"X-Frame-Options",
|
||||
"X-Powered-By",
|
||||
"X-XSS-Protection",
|
||||
"Last-Event-ID",
|
||||
"NEL",
|
||||
"Ping-From",
|
||||
"Ping-To",
|
||||
"Report-To",
|
||||
"Transfer-Encoding",
|
||||
"TE",
|
||||
"Trailer",
|
||||
"Sec-WebSocket-Key",
|
||||
"Sec-WebSocket-Extensions",
|
||||
"Sec-WebSocket-Accept",
|
||||
"Sec-WebSocket-Protocol",
|
||||
"Sec-WebSocket-Version",
|
||||
"Accept-Push-Policy",
|
||||
"Accept-Signature",
|
||||
"Alt-Svc",
|
||||
"Date",
|
||||
"Large-Allocation",
|
||||
"Link",
|
||||
"Push-Policy",
|
||||
"Retry-After",
|
||||
"Signature",
|
||||
"Signed-Headers",
|
||||
"Server-Timing",
|
||||
"SourceMap",
|
||||
"Upgrade",
|
||||
"X-DNS-Prefetch-Control",
|
||||
"X-Firefox-Spdy",
|
||||
"X-Pingback",
|
||||
"X-Requested-With",
|
||||
"X-Robots-Tag",
|
||||
"X-UA-Compatible",
|
||||
]
|
||||
@@ -1,310 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2019 GraphQL Contributors
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This JSON parser simply walks the input, generating an AST. Use this in lieu
|
||||
* of JSON.parse if you need character offset parse errors and an AST parse tree
|
||||
* with location information.
|
||||
*
|
||||
* If an error is encountered, a SyntaxError will be thrown, with properties:
|
||||
*
|
||||
* - message: string
|
||||
* - start: int - the start inclusive offset of the syntax error
|
||||
* - end: int - the end exclusive offset of the syntax error
|
||||
*
|
||||
*/
|
||||
export default function jsonParse(str) {
|
||||
string = str
|
||||
strLen = str.length
|
||||
start = end = lastEnd = -1
|
||||
ch()
|
||||
lex()
|
||||
const ast = parseObj()
|
||||
expect("EOF")
|
||||
return ast
|
||||
}
|
||||
|
||||
let string
|
||||
let strLen
|
||||
let start
|
||||
let end
|
||||
let lastEnd
|
||||
let code
|
||||
let kind
|
||||
|
||||
function parseObj() {
|
||||
const nodeStart = start
|
||||
const members = []
|
||||
expect("{")
|
||||
if (!skip("}")) {
|
||||
do {
|
||||
members.push(parseMember())
|
||||
} while (skip(","))
|
||||
expect("}")
|
||||
}
|
||||
return {
|
||||
kind: "Object",
|
||||
start: nodeStart,
|
||||
end: lastEnd,
|
||||
members,
|
||||
}
|
||||
}
|
||||
|
||||
function parseMember() {
|
||||
const nodeStart = start
|
||||
const key = kind === "String" ? curToken() : null
|
||||
expect("String")
|
||||
expect(":")
|
||||
const value = parseVal()
|
||||
return {
|
||||
kind: "Member",
|
||||
start: nodeStart,
|
||||
end: lastEnd,
|
||||
key,
|
||||
value,
|
||||
}
|
||||
}
|
||||
|
||||
function parseArr() {
|
||||
const nodeStart = start
|
||||
const values = []
|
||||
expect("[")
|
||||
if (!skip("]")) {
|
||||
do {
|
||||
values.push(parseVal())
|
||||
} while (skip(","))
|
||||
expect("]")
|
||||
}
|
||||
return {
|
||||
kind: "Array",
|
||||
start: nodeStart,
|
||||
end: lastEnd,
|
||||
values,
|
||||
}
|
||||
}
|
||||
|
||||
function parseVal() {
|
||||
switch (kind) {
|
||||
case "[":
|
||||
return parseArr()
|
||||
case "{":
|
||||
return parseObj()
|
||||
case "String":
|
||||
case "Number":
|
||||
case "Boolean":
|
||||
case "Null":
|
||||
const token = curToken()
|
||||
lex()
|
||||
return token
|
||||
}
|
||||
return expect("Value")
|
||||
}
|
||||
|
||||
function curToken() {
|
||||
return { kind, start, end, value: JSON.parse(string.slice(start, end)) }
|
||||
}
|
||||
|
||||
function expect(str) {
|
||||
if (kind === str) {
|
||||
lex()
|
||||
return
|
||||
}
|
||||
|
||||
let found
|
||||
if (kind === "EOF") {
|
||||
found = "[end of file]"
|
||||
} else if (end - start > 1) {
|
||||
found = `\`${string.slice(start, end)}\``
|
||||
} else {
|
||||
const match = string.slice(start).match(/^.+?\b/)
|
||||
found = `\`${match ? match[0] : string[start]}\``
|
||||
}
|
||||
|
||||
throw syntaxError(`Expected ${str} but found ${found}.`)
|
||||
}
|
||||
|
||||
function syntaxError(message) {
|
||||
return { message, start, end }
|
||||
}
|
||||
|
||||
function skip(k) {
|
||||
if (kind === k) {
|
||||
lex()
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
function ch() {
|
||||
if (end < strLen) {
|
||||
end++
|
||||
code = end === strLen ? 0 : string.charCodeAt(end)
|
||||
}
|
||||
}
|
||||
|
||||
function lex() {
|
||||
lastEnd = end
|
||||
|
||||
while (code === 9 || code === 10 || code === 13 || code === 32) {
|
||||
ch()
|
||||
}
|
||||
|
||||
if (code === 0) {
|
||||
kind = "EOF"
|
||||
return
|
||||
}
|
||||
|
||||
start = end
|
||||
|
||||
switch (code) {
|
||||
// "
|
||||
case 34:
|
||||
kind = "String"
|
||||
return readString()
|
||||
// -, 0-9
|
||||
case 45:
|
||||
case 48:
|
||||
case 49:
|
||||
case 50:
|
||||
case 51:
|
||||
case 52:
|
||||
case 53:
|
||||
case 54:
|
||||
case 55:
|
||||
case 56:
|
||||
case 57:
|
||||
kind = "Number"
|
||||
return readNumber()
|
||||
// f
|
||||
case 102:
|
||||
if (string.slice(start, start + 5) !== "false") {
|
||||
break
|
||||
}
|
||||
end += 4
|
||||
ch()
|
||||
|
||||
kind = "Boolean"
|
||||
return
|
||||
// n
|
||||
case 110:
|
||||
if (string.slice(start, start + 4) !== "null") {
|
||||
break
|
||||
}
|
||||
end += 3
|
||||
ch()
|
||||
|
||||
kind = "Null"
|
||||
return
|
||||
// t
|
||||
case 116:
|
||||
if (string.slice(start, start + 4) !== "true") {
|
||||
break
|
||||
}
|
||||
end += 3
|
||||
ch()
|
||||
|
||||
kind = "Boolean"
|
||||
return
|
||||
}
|
||||
|
||||
kind = string[start]
|
||||
ch()
|
||||
}
|
||||
|
||||
function readString() {
|
||||
ch()
|
||||
while (code !== 34 && code > 31) {
|
||||
if (code === 92) {
|
||||
// \
|
||||
ch()
|
||||
switch (code) {
|
||||
case 34: // "
|
||||
case 47: // /
|
||||
case 92: // \
|
||||
case 98: // b
|
||||
case 102: // f
|
||||
case 110: // n
|
||||
case 114: // r
|
||||
case 116: // t
|
||||
ch()
|
||||
break
|
||||
case 117: // u
|
||||
ch()
|
||||
readHex()
|
||||
readHex()
|
||||
readHex()
|
||||
readHex()
|
||||
break
|
||||
default:
|
||||
throw syntaxError("Bad character escape sequence.")
|
||||
}
|
||||
} else if (end === strLen) {
|
||||
throw syntaxError("Unterminated string.")
|
||||
} else {
|
||||
ch()
|
||||
}
|
||||
}
|
||||
|
||||
if (code === 34) {
|
||||
ch()
|
||||
return
|
||||
}
|
||||
|
||||
throw syntaxError("Unterminated string.")
|
||||
}
|
||||
|
||||
function readHex() {
|
||||
if (
|
||||
(code >= 48 && code <= 57) || // 0-9
|
||||
(code >= 65 && code <= 70) || // A-F
|
||||
(code >= 97 && code <= 102) // a-f
|
||||
) {
|
||||
return ch()
|
||||
}
|
||||
throw syntaxError("Expected hexadecimal digit.")
|
||||
}
|
||||
|
||||
function readNumber() {
|
||||
if (code === 45) {
|
||||
// -
|
||||
ch()
|
||||
}
|
||||
|
||||
if (code === 48) {
|
||||
// 0
|
||||
ch()
|
||||
} else {
|
||||
readDigits()
|
||||
}
|
||||
|
||||
if (code === 46) {
|
||||
// .
|
||||
ch()
|
||||
readDigits()
|
||||
}
|
||||
|
||||
if (code === 69 || code === 101) {
|
||||
// E e
|
||||
ch()
|
||||
if (code === 43 || code === 45) {
|
||||
// + -
|
||||
ch()
|
||||
}
|
||||
readDigits()
|
||||
}
|
||||
}
|
||||
|
||||
function readDigits() {
|
||||
if (code < 48 || code > 57) {
|
||||
// 0 - 9
|
||||
throw syntaxError("Expected decimal digit.")
|
||||
}
|
||||
do {
|
||||
ch()
|
||||
} while (code >= 48 && code <= 57) // 0 - 9
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
import AxiosStrategy, { cancelRunningAxiosRequest } from "./strategies/AxiosStrategy"
|
||||
import ExtensionStrategy, {
|
||||
cancelRunningExtensionRequest,
|
||||
hasExtensionInstalled,
|
||||
} from "./strategies/ExtensionStrategy"
|
||||
|
||||
export const cancelRunningRequest = (store) => {
|
||||
if (isExtensionsAllowed(store) && hasExtensionInstalled()) {
|
||||
cancelRunningExtensionRequest()
|
||||
} else {
|
||||
cancelRunningAxiosRequest()
|
||||
}
|
||||
}
|
||||
|
||||
const isExtensionsAllowed = ({ state }) =>
|
||||
typeof state.postwoman.settings.EXTENSIONS_ENABLED === "undefined" ||
|
||||
state.postwoman.settings.EXTENSIONS_ENABLED
|
||||
|
||||
const runAppropriateStrategy = (req, store) => {
|
||||
if (isExtensionsAllowed(store) && hasExtensionInstalled()) {
|
||||
return ExtensionStrategy(req, store)
|
||||
}
|
||||
|
||||
return AxiosStrategy(req, store)
|
||||
}
|
||||
|
||||
export const sendNetworkRequest = (req, store) =>
|
||||
runAppropriateStrategy(req, store).finally(() => window.$nuxt.$loading.finish())
|
||||
@@ -1,3 +0,0 @@
|
||||
export function getPlatformSpecialKey() {
|
||||
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform) ? "⌘" : "Ctrl"
|
||||
}
|
||||
@@ -1,147 +0,0 @@
|
||||
const PASS = "PASS"
|
||||
const FAIL = "FAIL"
|
||||
const ERROR = "ERROR"
|
||||
|
||||
const styles = {
|
||||
[PASS]: { icon: "check", class: "success-response" },
|
||||
[FAIL]: { icon: "close", class: "cl-error-response" },
|
||||
[ERROR]: { icon: "close", class: "cl-error-response" },
|
||||
none: { icon: "", class: "" },
|
||||
}
|
||||
|
||||
// TODO: probably have to use a more global state for `test`
|
||||
|
||||
export default function runTestScriptWithVariables(script, variables) {
|
||||
let pw = {
|
||||
_errors: [],
|
||||
_testReports: [],
|
||||
_report: "",
|
||||
expect(value) {
|
||||
try {
|
||||
return expect(value, this._testReports)
|
||||
} catch (e) {
|
||||
pw._testReports.push({ result: ERROR, message: e })
|
||||
}
|
||||
},
|
||||
test: (descriptor, func) => test(descriptor, func, pw._testReports),
|
||||
// globals that the script is allowed to have access to.
|
||||
}
|
||||
Object.assign(pw, variables)
|
||||
|
||||
// run pre-request script within this function so that it has access to the pw object.
|
||||
new Function("pw", script)(pw)
|
||||
//
|
||||
const testReports = pw._testReports.map((item) => {
|
||||
if (item.result) {
|
||||
item.styles = styles[item.result]
|
||||
} else {
|
||||
item.styles = styles.none
|
||||
}
|
||||
return item
|
||||
})
|
||||
return { report: pw._report, errors: pw._errors, testResults: testReports }
|
||||
}
|
||||
|
||||
function test(descriptor, func, _testReports) {
|
||||
_testReports.push({ startBlock: descriptor })
|
||||
try {
|
||||
func()
|
||||
} catch (e) {
|
||||
_testReports.push({ result: ERROR, message: e })
|
||||
}
|
||||
_testReports.push({ endBlock: true })
|
||||
|
||||
// TODO: Organize and generate text report of each {descriptor: true} section in testReports.
|
||||
// add checkmark or x depending on if each testReport is pass=true or pass=false
|
||||
}
|
||||
|
||||
function expect(expectValue, _testReports) {
|
||||
return new Expectation(expectValue, null, _testReports)
|
||||
}
|
||||
|
||||
class Expectation {
|
||||
constructor(expectValue, _not, _testReports) {
|
||||
this.expectValue = expectValue
|
||||
this.not = _not || new Expectation(this.expectValue, true, _testReports)
|
||||
this._testReports = _testReports // this values is used within Test.it, which wraps Expectation and passes _testReports value.
|
||||
this._satisfies = function (expectValue, targetValue) {
|
||||
// Used for testing if two values match the expectation, which could be === OR !==, depending on if not
|
||||
// was used. Expectation#_satisfies prevents the need to have an if(this.not) branch in every test method.
|
||||
// Signature is _satisfies([expectValue,] targetValue): if only one argument is given, it is assumed the targetValue, and expectValue is set to this.expectValue
|
||||
if (!targetValue) {
|
||||
targetValue = expectValue
|
||||
expectValue = this.expectValue
|
||||
}
|
||||
if (this.not === true) {
|
||||
// test the inverse. this.not is always truthly, but an Expectation that is inverted will always be strictly `true`
|
||||
return expectValue !== targetValue
|
||||
} else {
|
||||
return expectValue === targetValue
|
||||
}
|
||||
}
|
||||
}
|
||||
_fmtNot(message) {
|
||||
// given a string with "(not)" in it, replaces with "not" or "", depending if the expectation is expecting the positive or inverse (this._not)
|
||||
if (this.not === true) {
|
||||
return message.replace("(not)", "not ")
|
||||
} else {
|
||||
return message.replace("(not)", "")
|
||||
}
|
||||
}
|
||||
_fail(message) {
|
||||
this._testReports.push({ result: FAIL, message })
|
||||
}
|
||||
_pass(message) {
|
||||
this._testReports.push({ result: PASS })
|
||||
}
|
||||
// TEST METHODS DEFINED BELOW
|
||||
// these are the usual methods that would follow expect(...)
|
||||
toBe(value) {
|
||||
return this._satisfies(value)
|
||||
? this._pass()
|
||||
: this._fail(this._fmtNot(`Expected ${this.expectValue} (not)to be ${value}`))
|
||||
}
|
||||
toHaveProperty(value) {
|
||||
return this._satisfies(this.expectValue.hasOwnProperty(value), true)
|
||||
? this._pass()
|
||||
: this._fail(
|
||||
this._fmtNot(`Expected object ${this.expectValue} to (not)have property ${value}`)
|
||||
)
|
||||
}
|
||||
toBeLevel2xx() {
|
||||
const code = parseInt(this.expectValue)
|
||||
if (Number.isNaN(code)) {
|
||||
return this._fail(`Expected 200-level status but could not parse value ${this.expectValue}`)
|
||||
}
|
||||
return this._satisfies(code >= 200 && code < 300)
|
||||
? this._pass()
|
||||
: this._fail(this._fmtNot(`Expected ${this.expectValue} to (not)be 200-level status`))
|
||||
}
|
||||
toBeLevel3xx() {
|
||||
const code = parseInt(this.expectValue)
|
||||
if (Number.isNaN(code)) {
|
||||
return this._fail(`Expected 300-level status but could not parse value ${this.expectValue}`)
|
||||
}
|
||||
return this._satisfies(code >= 300 && code < 400)
|
||||
? this._pass()
|
||||
: this._fail(this._fmtNot(`Expected ${this.expectValue} to (not)be 300-level status`))
|
||||
}
|
||||
toBeLevel4xx() {
|
||||
const code = parseInt(this.expectValue)
|
||||
if (Number.isNaN(code)) {
|
||||
return this._fail(`Expected 400-level status but could not parse value ${this.expectValue}`)
|
||||
}
|
||||
return this._satisfies(code >= 400 && code < 500)
|
||||
? this._pass()
|
||||
: this._fail(this._fmtNot(`Expected ${this.expectValue} to (not)be 400-level status`))
|
||||
}
|
||||
toBeLevel5xx() {
|
||||
const code = parseInt(this.expectValue)
|
||||
if (Number.isNaN(code)) {
|
||||
return this._fail(`Expected 500-level status but could not parse value ${this.expectValue}`)
|
||||
}
|
||||
return this._satisfies(code >= 500 && code < 600)
|
||||
? this._pass()
|
||||
: this._fail(this._fmtNot(`Expected ${this.expectValue} to (not)be 500-level status`))
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
export default function getEnvironmentVariablesFromScript(script) {
|
||||
let _variables = {}
|
||||
|
||||
// the pw object is the proxy by which pre-request scripts can pass variables to the request.
|
||||
// for security and control purposes, this is the only way a pre-request script should modify variables.
|
||||
let pw = {
|
||||
environment: {
|
||||
set: (key, value) => (_variables[key] = value),
|
||||
},
|
||||
env: {
|
||||
set: (key, value) => (_variables[key] = value),
|
||||
},
|
||||
// globals that the script is allowed to have access to.
|
||||
}
|
||||
|
||||
// run pre-request script within this function so that it has access to the pw object.
|
||||
new Function("pw", script)(pw)
|
||||
|
||||
return _variables
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
export function hasPathParams(params) {
|
||||
return params.some(({ type }) => type === "path")
|
||||
}
|
||||
|
||||
export function addPathParamsToVariables(params, variables) {
|
||||
params
|
||||
.filter(({ key }) => !!key)
|
||||
.filter(({ type }) => type === "path")
|
||||
.forEach(({ key, value }) => (variables[key] = value))
|
||||
return variables
|
||||
}
|
||||
|
||||
export function getQueryParams(params) {
|
||||
return params.filter(({ key }) => !!key).filter(({ type }) => type != "path")
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
import axios from "axios"
|
||||
|
||||
let cancelSource = axios.CancelToken.source()
|
||||
|
||||
export const cancelRunningAxiosRequest = () => {
|
||||
cancelSource.cancel()
|
||||
|
||||
// Create a new cancel token
|
||||
cancelSource = axios.CancelToken.source()
|
||||
}
|
||||
|
||||
const axiosWithProxy = async (req, { state }) => {
|
||||
try {
|
||||
const { data } = await axios.post(
|
||||
state.postwoman.settings.PROXY_URL || "https://postwoman.apollosoftware.xyz/",
|
||||
req,
|
||||
{
|
||||
cancelToken: cancelSource.token,
|
||||
}
|
||||
)
|
||||
return data
|
||||
} catch (e) {
|
||||
// Check if the throw is due to a cancellation
|
||||
if (axios.isCancel(e)) {
|
||||
throw "cancellation"
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const axiosWithoutProxy = async (req, _store) => {
|
||||
try {
|
||||
const res = await axios({
|
||||
...req,
|
||||
cancelToken: cancelSource.token,
|
||||
transformResponse: [
|
||||
(data, headers) => {
|
||||
// If the response has a JSON content type, try parsing it
|
||||
if (
|
||||
headers["content-type"] &&
|
||||
(headers["content-type"].startsWith("application/json") ||
|
||||
headers["content-type"].startsWith("application/vnd.api+json") ||
|
||||
headers["content-type"].startsWith("application/hal+json"))
|
||||
) {
|
||||
try {
|
||||
const jsonData = JSON.parse(data)
|
||||
return jsonData
|
||||
} catch (e) {
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
// Else return the string itself without any transformations
|
||||
return data
|
||||
},
|
||||
],
|
||||
})
|
||||
return res
|
||||
} catch (e) {
|
||||
if (axios.isCancel(e)) {
|
||||
throw "cancellation"
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const axiosStrategy = (req, store) => {
|
||||
if (store.state.postwoman.settings.PROXY_ENABLED) {
|
||||
return axiosWithProxy(req, store)
|
||||
}
|
||||
return axiosWithoutProxy(req, store)
|
||||
}
|
||||
|
||||
export default axiosStrategy
|
||||
@@ -1,37 +0,0 @@
|
||||
export const hasExtensionInstalled = () =>
|
||||
typeof window.__POSTWOMAN_EXTENSION_HOOK__ !== "undefined"
|
||||
|
||||
export const hasChromeExtensionInstalled = () =>
|
||||
hasExtensionInstalled() && /Chrome/i.test(navigator.userAgent) && /Google/i.test(navigator.vendor)
|
||||
|
||||
export const hasFirefoxExtensionInstalled = () =>
|
||||
hasExtensionInstalled() && /Firefox/i.test(navigator.userAgent)
|
||||
|
||||
export const cancelRunningExtensionRequest = () => {
|
||||
if (hasExtensionInstalled() && window.__POSTWOMAN_EXTENSION_HOOK__.cancelRunningRequest) {
|
||||
window.__POSTWOMAN_EXTENSION_HOOK__.cancelRunningRequest()
|
||||
}
|
||||
}
|
||||
|
||||
const extensionWithProxy = async (req, { state }) => {
|
||||
const { data } = await window.__POSTWOMAN_EXTENSION_HOOK__.sendRequest({
|
||||
method: "post",
|
||||
url: state.postwoman.settings.PROXY_URL || "https://postwoman.apollosoftware.xyz/",
|
||||
data: req,
|
||||
})
|
||||
return data
|
||||
}
|
||||
|
||||
const extensionWithoutProxy = async (req, _store) => {
|
||||
const res = await window.__POSTWOMAN_EXTENSION_HOOK__.sendRequest(req)
|
||||
return res
|
||||
}
|
||||
|
||||
const extensionStrategy = (req, store) => {
|
||||
if (store.state.postwoman.settings.PROXY_ENABLED) {
|
||||
return extensionWithProxy(req, store)
|
||||
}
|
||||
return extensionWithoutProxy(req, store)
|
||||
}
|
||||
|
||||
export default extensionStrategy
|
||||
@@ -1,115 +0,0 @@
|
||||
export function defineGQLLanguageMode(ace) {
|
||||
// Highlighting
|
||||
ace.define(
|
||||
"ace/mode/gql-query-highlight",
|
||||
["require", "exports", "ace/lib/oop", "ace/mode/text_highlight_rules"],
|
||||
(aceRequire, exports) => {
|
||||
const oop = aceRequire("ace/lib/oop")
|
||||
|
||||
const TextHighlightRules = aceRequire("ace/mode/text_highlight_rules").TextHighlightRules
|
||||
|
||||
const GQLQueryTextHighlightRules = function () {
|
||||
const keywords =
|
||||
"type|interface|union|enum|schema|input|implements|extends|scalar|fragment|query|mutation|subscription"
|
||||
|
||||
const dataTypes = "Int|Float|String|ID|Boolean"
|
||||
|
||||
const literalValues = "true|false|null"
|
||||
|
||||
const escapeRe = /\\(?:u[\da-fA-f]{4}|.)/
|
||||
|
||||
const keywordMapper = this.createKeywordMapper(
|
||||
{
|
||||
keyword: keywords,
|
||||
"storage.type": dataTypes,
|
||||
"constant.language": literalValues,
|
||||
},
|
||||
"identifier"
|
||||
)
|
||||
|
||||
this.$rules = {
|
||||
start: [
|
||||
{
|
||||
token: "comment",
|
||||
regex: "#.*$",
|
||||
},
|
||||
{
|
||||
token: "paren.lparen",
|
||||
regex: /[\[({]/,
|
||||
next: "start",
|
||||
},
|
||||
{
|
||||
token: "paren.rparen",
|
||||
regex: /[\])}]/,
|
||||
},
|
||||
{
|
||||
token: keywordMapper,
|
||||
regex: "[a-zA-Z_][a-zA-Z0-9_$]*\\b",
|
||||
},
|
||||
{
|
||||
token: "string", // character
|
||||
regex: `'(?:${escapeRe}|.)?'`,
|
||||
},
|
||||
{
|
||||
token: "string.start",
|
||||
regex: '"',
|
||||
stateName: "qqstring",
|
||||
next: [
|
||||
{ token: "string", regex: /\\\s*$/, next: "qqstring" },
|
||||
{ token: "constant.language.escape", regex: escapeRe },
|
||||
{ token: "string.end", regex: '"|$', next: "start" },
|
||||
{ defaultToken: "string" },
|
||||
],
|
||||
},
|
||||
{
|
||||
token: "string.start",
|
||||
regex: "'",
|
||||
stateName: "singleQuoteString",
|
||||
next: [
|
||||
{ token: "string", regex: /\\\s*$/, next: "singleQuoteString" },
|
||||
{ token: "constant.language.escape", regex: escapeRe },
|
||||
{ token: "string.end", regex: "'|$", next: "start" },
|
||||
{ defaultToken: "string" },
|
||||
],
|
||||
},
|
||||
{
|
||||
token: "constant.numeric",
|
||||
regex: /\d+\.?\d*[eE]?[\+\-]?\d*/,
|
||||
},
|
||||
{
|
||||
token: "variable",
|
||||
regex: /\$[_A-Za-z][_0-9A-Za-z]*/,
|
||||
},
|
||||
],
|
||||
}
|
||||
this.normalizeRules()
|
||||
}
|
||||
|
||||
oop.inherits(GQLQueryTextHighlightRules, TextHighlightRules)
|
||||
|
||||
exports.GQLQueryTextHighlightRules = GQLQueryTextHighlightRules
|
||||
}
|
||||
)
|
||||
|
||||
// Language Mode Definition
|
||||
ace.define(
|
||||
"ace/mode/gql-query",
|
||||
["require", "exports", "ace/mode/text", "ace/mode/gql-query-highlight"],
|
||||
(aceRequire, exports) => {
|
||||
const oop = aceRequire("ace/lib/oop")
|
||||
const TextMode = aceRequire("ace/mode/text").Mode
|
||||
const GQLQueryTextHighlightRules = aceRequire("ace/mode/gql-query-highlight")
|
||||
.GQLQueryTextHighlightRules
|
||||
const FoldMode = aceRequire("ace/mode/folding/cstyle").FoldMode
|
||||
|
||||
const Mode = function () {
|
||||
this.HighlightRules = GQLQueryTextHighlightRules
|
||||
this.foldingRules = new FoldMode()
|
||||
}
|
||||
|
||||
oop.inherits(Mode, TextMode)
|
||||
|
||||
exports.Mode = Mode
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
export default function parseTemplateString(string, variables) {
|
||||
if (!variables || !string) {
|
||||
return string
|
||||
}
|
||||
const searchTerm = /<<([^>]*)>>/g // "<<myVariable>>"
|
||||
return decodeURI(string).replace(searchTerm, (match, p1) => variables[p1] || "")
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
export const knownContentTypes = [
|
||||
"application/json",
|
||||
"application/vnd.api+json",
|
||||
"application/hal+json",
|
||||
"application/xml",
|
||||
"application/x-www-form-urlencoded",
|
||||
"text/html",
|
||||
"text/plain",
|
||||
]
|
||||
|
||||
export function isJSONContentType(contentType) {
|
||||
if (contentType.includes(";")) {
|
||||
const [justContentType] = contentType.split(";")
|
||||
|
||||
return (
|
||||
justContentType === "application/json" ||
|
||||
justContentType === "application/vnd.api+json" ||
|
||||
justContentType === "application/hal+json"
|
||||
)
|
||||
} else {
|
||||
return (
|
||||
contentType === "application/json" ||
|
||||
contentType === "application/vnd.api+json" ||
|
||||
contentType === "application/hal+json"
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
// Debounce is a higher order function which makes its enclosed function be executed
|
||||
// only if the function wasn't called again till 'delay' time has passed, this helps reduce impact of heavy working
|
||||
// functions which might be called frequently
|
||||
// NOTE : Don't use lambda functions as this doesn't get bound properly in them, use the 'function (args) {}' format
|
||||
const debounce = (func, delay) => {
|
||||
let inDebounce
|
||||
return function () {
|
||||
const context = this
|
||||
const args = arguments
|
||||
clearTimeout(inDebounce)
|
||||
inDebounce = setTimeout(() => func.apply(context, args), delay)
|
||||
}
|
||||
}
|
||||
|
||||
export default debounce
|
||||
@@ -1,12 +0,0 @@
|
||||
export function getSourcePrefix(source) {
|
||||
const sourceEmojis = {
|
||||
// Source used for info messages.
|
||||
info: "\tℹ️ [INFO]:\t",
|
||||
// Source used for client to server messages.
|
||||
client: "\t👽 [SENT]:\t",
|
||||
// Source used for server to client messages.
|
||||
server: "\t📥 [RECEIVED]:\t",
|
||||
}
|
||||
if (Object.keys(sourceEmojis).includes(source)) return sourceEmojis[source]
|
||||
return ""
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
export function parseUrlAndPath(value) {
|
||||
let result = {}
|
||||
try {
|
||||
let url = new URL(value)
|
||||
result.url = url.origin
|
||||
result.path = url.pathname
|
||||
} catch (error) {
|
||||
let uriRegex = value.match(/^((http[s]?:\/\/)?(<<[^\/]+>>)?[^\/]*|)(\/?.*)$/)
|
||||
result.url = uriRegex[1]
|
||||
result.path = uriRegex[4]
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
const [wsRegexIP, wsRegexHostname] = generateREForProtocol("^(wss?:\\/\\/)?")
|
||||
const [sseRegexIP, sseRegexHostname] = generateREForProtocol("^(https?:\\/\\/)?")
|
||||
const [socketioRegexIP, socketioRegexHostname] = generateREForProtocol(
|
||||
"^((wss?:\\/\\/)|(https?:\\/\\/))?"
|
||||
)
|
||||
|
||||
function generateREForProtocol(protocol) {
|
||||
return [
|
||||
new RegExp(
|
||||
`${protocol}(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`
|
||||
),
|
||||
new RegExp(
|
||||
`${protocol}(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]).)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9/])$`
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
/**
|
||||
* valid url for ws/wss
|
||||
*/
|
||||
export function wsValid(url) {
|
||||
return wsRegexIP.test(url) || wsRegexHostname.test(url)
|
||||
}
|
||||
|
||||
/**
|
||||
* valid url for http/https
|
||||
*/
|
||||
export function httpValid(url) {
|
||||
return sseRegexIP.test(url) || sseRegexHostname.test(url)
|
||||
}
|
||||
|
||||
/**
|
||||
* valid url for ws/wss/http/https
|
||||
*/
|
||||
export function socketioValid(url) {
|
||||
return socketioRegexIP.test(url) || socketioRegexHostname.test(url)
|
||||
}
|
||||
Reference in New Issue
Block a user