"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "packages/hoppscotch-app/helpers/import-export/import/postman.ts" between
hoppscotch-2.2.1.tar.gz and hoppscotch-3.0.0.tar.gz

About: Hoppscotch is a light-weight, web based API development suite.

postman.ts  (hoppscotch-2.2.1):postman.ts  (hoppscotch-3.0.0)
import IconPostman from "~icons/hopp/postman"
import { import {
Collection as PMCollection, Collection as PMCollection,
FormParam,
Item, Item,
ItemGroup, ItemGroup,
QueryParam, QueryParam,
RequestAuthDefinition, RequestAuthDefinition,
VariableDefinition, VariableDefinition,
} from "postman-collection" } from "postman-collection"
import { import {
HoppRESTAuth, HoppRESTAuth,
HoppRESTHeader, HoppRESTHeader,
HoppRESTParam, HoppRESTParam,
HoppRESTReqBody, HoppRESTReqBody,
HoppRESTRequest, HoppRESTRequest,
makeRESTRequest, makeRESTRequest,
HoppCollection, HoppCollection,
makeCollection, makeCollection,
ValidContentTypes,
knownContentTypes,
FormDataKeyValue,
} from "@hoppscotch/data" } from "@hoppscotch/data"
import { pipe, flow } from "fp-ts/function" import { pipe, flow } from "fp-ts/function"
import * as S from "fp-ts/string" import * as S from "fp-ts/string"
import * as A from "fp-ts/Array" import * as A from "fp-ts/Array"
import * as O from "fp-ts/Option" import * as O from "fp-ts/Option"
import * as TE from "fp-ts/TaskEither" import * as TE from "fp-ts/TaskEither"
import { step } from "../steps" import { step } from "../steps"
import { defineImporter, IMPORTER_INVALID_FILE_FORMAT } from "." import { defineImporter, IMPORTER_INVALID_FILE_FORMAT } from "."
import { PMRawLanguage } from "~/types/pm-coll-exts"
import { stringArrayJoin } from "~/helpers/functional/array"
const safeParseJSON = (jsonStr: string) => O.tryCatch(() => JSON.parse(jsonStr)) const safeParseJSON = (jsonStr: string) => O.tryCatch(() => JSON.parse(jsonStr))
const isPMItem = (x: unknown): x is Item => Item.isItem(x) const isPMItem = (x: unknown): x is Item => Item.isItem(x)
const replacePMVarTemplating = flow( const replacePMVarTemplating = flow(
S.replace(/{{\s*/g, "<<"), S.replace(/{{\s*/g, "<<"),
S.replace(/\s*}}/g, ">>") S.replace(/\s*}}/g, ">>")
) )
const PMRawLanguageOptionsToContentTypeMap: Record<
PMRawLanguage,
ValidContentTypes
> = {
text: "text/plain",
javascript: "text/plain",
json: "application/json",
html: "text/html",
xml: "application/xml",
}
const isPMItemGroup = (x: unknown): x is ItemGroup<Item> => const isPMItemGroup = (x: unknown): x is ItemGroup<Item> =>
ItemGroup.isItemGroup(x) ItemGroup.isItemGroup(x)
const readPMCollection = (def: string) => const readPMCollection = (def: string) =>
pipe( pipe(
def, def,
safeParseJSON, safeParseJSON,
O.chain((data) => O.tryCatch(() => new PMCollection(data))) O.chain((data) => O.tryCatch(() => new PMCollection(data)))
) )
skipping to change at line 150 skipping to change at line 166
token: replacePMVarTemplating( token: replacePMVarTemplating(
getVariableValue(auth.oauth2, "accessToken") ?? "" getVariableValue(auth.oauth2, "accessToken") ?? ""
), ),
oidcDiscoveryURL: "", oidcDiscoveryURL: "",
} }
} }
return { authType: "none", authActive: true } return { authType: "none", authActive: true }
} }
type PMFormDataParamType = FormParam & {
type: "file" | "text"
}
const getHoppReqBody = (item: Item): HoppRESTReqBody => { const getHoppReqBody = (item: Item): HoppRESTReqBody => {
if (!item.request.body) return { contentType: null, body: null } if (!item.request.body) return { contentType: null, body: null }
// TODO: Implement
const body = item.request.body const body = item.request.body
if (body.mode === "formdata") { if (body.mode === "formdata") {
return { return {
contentType: "multipart/form-data", contentType: "multipart/form-data",
body: body: pipe(
(body.formdata?.all() as PMFormDataParamType[]).map((param) => ({ body.formdata?.all() ?? [],
key: replacePMVarTemplating(param.key), A.map(
value: replacePMVarTemplating( (param) =>
param.type === "text" ? (param.value as string) : "" <FormDataKeyValue>{
), key: replacePMVarTemplating(param.key),
active: !param.disabled, value: replacePMVarTemplating(
isFile: false, // TODO: Preserve isFile state ? param.type === "text" ? (param.value as string) : ""
})) ?? [], ),
active: !param.disabled,
isFile: false, // TODO: Preserve isFile state ?
}
)
),
} }
} else if (body.mode === "urlencoded") { } else if (body.mode === "urlencoded") {
return { return {
contentType: "application/x-www-form-urlencoded", contentType: "application/x-www-form-urlencoded",
body: body: pipe(
body.urlencoded body.urlencoded?.all() ?? [],
?.all() A.map(
.map( (param) =>
(param) => `${replacePMVarTemplating(
`${replacePMVarTemplating( param.key ?? ""
param.key ?? "" )}: ${replacePMVarTemplating(param.value ?? "")}`
)}: ${replacePMVarTemplating(param.value ?? "")}` ),
) stringArrayJoin("\n")
.join("\n") ?? "", ),
} }
} else if (body.mode === "raw") { } else if (body.mode === "raw") {
// Find content type from the content type header return pipe(
const contentType = getHoppReqHeaders(item).find( O.Do,
({ key }) => key.toLowerCase() === "content-type"
)?.value // Extract content-type
O.bind("contentType", () =>
if (contentType && body.raw !== undefined && body.raw !== null) pipe(
return { // Get the info from the content-type header
contentType: contentType as any, getHoppReqHeaders(item),
body: replacePMVarTemplating(body.raw), A.findFirst(({ key }) => key.toLowerCase() === "content-type"),
} O.map((x) => x.value),
else return { contentType: null, body: null } // TODO: Any sort of recovery
? // Make sure it is a content-type Hopp can work with
O.filter(
(contentType): contentType is ValidContentTypes =>
contentType in knownContentTypes
),
// Back-up plan, assume language from raw language defintion
O.alt(() =>
pipe(
body.options?.raw?.language,
O.fromNullable,
O.map((lang) => PMRawLanguageOptionsToContentTypeMap[lang])
)
),
// If that too failed, just assume "text/plain"
O.getOrElse((): ValidContentTypes => "text/plain"),
O.of
)
),
// Extract and parse body
O.bind("body", () =>
pipe(body.raw, O.fromNullable, O.map(replacePMVarTemplating))
),
// Return null content-type if failed, else return parsed
O.match(
() =>
<HoppRESTReqBody>{
contentType: null,
body: null,
},
({ contentType, body }) =>
<HoppRESTReqBody>{
contentType,
body,
}
)
)
} }
// TODO: File // TODO: File
// TODO: GraphQL ? // TODO: GraphQL ?
return { contentType: null, body: null } return { contentType: null, body: null }
} }
const getHoppReqURL = (item: Item): string => const getHoppReqURL = (item: Item): string =>
pipe( pipe(
item.request.url.toString(true), item.request.url.toString(false),
S.replace(/\?.+/g, ""), S.replace(/\?.+/g, ""),
replacePMVarTemplating replacePMVarTemplating
) )
const getHoppRequest = (item: Item): HoppRESTRequest => { const getHoppRequest = (item: Item): HoppRESTRequest => {
return makeRESTRequest({ return makeRESTRequest({
name: item.name, name: item.name,
endpoint: getHoppReqURL(item), endpoint: getHoppReqURL(item),
method: item.request.method.toUpperCase(), method: item.request.method.toUpperCase(),
headers: getHoppReqHeaders(item), headers: getHoppReqHeaders(item),
skipping to change at line 244 skipping to change at line 301
ig.items.all(), ig.items.all(),
A.filter(isPMItemGroup), A.filter(isPMItemGroup),
A.map(getHoppFolder) A.map(getHoppFolder)
), ),
requests: pipe(ig.items.all(), A.filter(isPMItem), A.map(getHoppRequest)), requests: pipe(ig.items.all(), A.filter(isPMItem), A.map(getHoppRequest)),
}) })
export const getHoppCollection = (coll: PMCollection) => getHoppFolder(coll) export const getHoppCollection = (coll: PMCollection) => getHoppFolder(coll)
export default defineImporter({ export default defineImporter({
id: "postman",
name: "import.from_postman", name: "import.from_postman",
icon: "postman", applicableTo: ["my-collections", "team-collections", "url-import"],
icon: IconPostman,
steps: [ steps: [
step({ step({
stepName: "FILE_IMPORT", stepName: "FILE_IMPORT",
metadata: { metadata: {
caption: "import.from_postman_description", caption: "import.from_postman_description",
acceptedFileTypes: ".json", acceptedFileTypes: ".json",
}, },
}), }),
] as const, ] as const,
importer: ([fileContent]) => importer: ([fileContent]) =>
 End of changes. 13 change blocks. 
39 lines changed or deleted 97 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)