UNPKG

@reecem/custom-type-api

Version:

API interface for the Prismic.io Custom Type API (beta)

1 lines 10.2 kB
{"version":3,"file":"index.cjs","sources":["../src/API/Slices/index.ts","../src/utils/routes.ts","../src/API/Types/index.ts","../src/utils/interface.ts","../src/index.ts","../src/utils/login.ts"],"sourcesContent":["import { Response } from \"node-fetch\";\nimport { SliceModel } from \"../../Models/SliceModel\";\nimport HttpFetch from \"../../utils/interface\";\nimport { slices as sliceRoute} from '../../utils/routes';\nimport ApiInterface from \"../api-interface\";\n\nclass Slices implements ApiInterface {\n private repository: string;\n private http: HttpFetch;\n\n constructor(repository: string, http: HttpFetch) {\n this.repository = repository;\n this.http = http.withHeaders({\n 'repository': this.repository,\n });\n }\n\n public async getOne(slice: string): Promise<SliceModel> {\n try {\n\n const response = await (await this.http.get(sliceRoute.show(slice)));\n\n if (response.status === 403) {\n let json = await response.json()\n\n let message = json?.message as string\n\n throw message.search('not a valid key=value pair (missing equal-sign) in Authorization header:')\n ? new Error('Incorrect Url or Token issue')\n : new Error('Unauthorized');\n }\n\n const sliceModel: SliceModel = await response.json();\n\n return sliceModel;\n } catch (error) {\n throw new Error(`[ERROR] Unable to fetch the slice: ${slice}`)\n }\n }\n\n public async getAll(): Promise<SliceModel[]> {\n try {\n\n const response = await this.http.get(sliceRoute.index);\n\n const slices: SliceModel[] = await response.json();\n\n return slices;\n } catch (error) {\n throw new Error(error)\n }\n }\n\n public async insert(slice: SliceModel): Promise<Response> {\n\n return await this.http.post(sliceRoute.insert, slice);\n }\n\n public async update(slice: SliceModel): Promise<Response> {\n\n return await this.http.post(sliceRoute.update, slice);\n }\n}\n\nexport default Slices;\n","/**\n * Route definition file for the custom type API\n *\n * @exports\n */\nexport const slices = {\n index: \"https://customtypes.prismic.io/slices\",\n show: (slice: string) => `https://customtypes.prismic.io/slices/${slice}`,\n insert: \"https://customtypes.prismic.io/slices/insert\",\n update: \"https://customtypes.prismic.io/slices/update\"\n}\n\nexport const types = {\n index: \"https://customtypes.prismic.io/customtypes\",\n show: (type: string) => `https://customtypes.prismic.io/customtypes/${type}`,\n insert: \"https://customtypes.prismic.io/customtypes/insert\",\n update: \"https://customtypes.prismic.io/customtypes/update\"\n}\n\nexport default {\n slices,\n types\n}\n","import { Response } from \"node-fetch\";\nimport { TypeModel } from \"../../Models/TypeModel\";\nimport HttpFetch from \"../../utils/interface\";\nimport { types as typeRoute } from \"../../utils/routes\";\nimport ApiInterface from \"../api-interface\";\n\nclass Types implements ApiInterface {\n private repository: string;\n private http: HttpFetch;\n\n constructor(repository: string, http: HttpFetch) {\n this.repository = repository\n\n this.http = http.withHeaders({\n 'repository': this.repository,\n });\n }\n\n public async getOne(customType: string): Promise<TypeModel> {\n\n try {\n const response = await this.http.get(typeRoute.show(customType));\n\n const type:TypeModel = await response.json();\n\n return type;\n } catch (error) {\n throw new Error(error)\n }\n }\n\n public async getAll(): Promise<TypeModel[]> {\n\n try {\n const response = await this.http.get(typeRoute.index);\n const types: TypeModel[] = await response.json()\n\n return types;\n } catch (error) {\n throw new Error(error)\n }\n }\n\n\n public async insert(customType: TypeModel): Promise<Response> {\n\n if (typeof customType.json !== 'object') {\n throw new Error(\"JSON field of custom type isn't and object. Please check if it is correct\")\n }\n\n try {\n\n const insertResponse = await this.http.post(typeRoute.insert, customType);\n const result = await insertResponse.json()\n\n return result;\n\n } catch (error) {\n throw new Error(error)\n }\n }\n\n public async update(customType: TypeModel): Promise<Response> {\n\n return await this.http.post(typeRoute.update, customType);\n }\n}\n\nexport default Types;\n","import fetch, { Headers, RequestInit, Response } from 'node-fetch';\n\ninterface Handler {\n get(url: string): Promise<Response>,\n post(url: string, body: object): Promise<Response>,\n}\n\nclass HttpFetch implements Handler{\n headers: Headers;\n\n constructor(token: string) {\n this.headers = new Headers;\n\n this.headers.append('Authorization', `Bearer ${token}`)\n }\n\n request(method: string, body?: any | object): RequestInit {\n\n return {\n method: method,\n headers: this.headers,\n body,\n redirect: 'follow',\n };\n }\n\n async post(url: string, body: object): Promise<Response> {\n\n return await fetch(url, this.request('POST', JSON.stringify(body)));\n }\n\n async get(url: string): Promise<Response> {\n\n return await fetch(url, this.request('GET'))\n }\n\n withHeaders(newHeaders: {[key: string]: any}) {\n for (const header in newHeaders) {\n\n if (this.headers?.has(header)) {\n if (this.headers?.get(header) === newHeaders[header]) return this;\n }\n\n this.headers?.append(header, newHeaders[header])\n }\n\n return this;\n }\n}\n\nexport default HttpFetch\n","import Slices from \"./API/Slices\";\nimport Types from \"./API/Types\";\nimport login from \"./utils/login\";\n\nimport HttpFetch from \"./utils/interface\";\n\nclass Api {\n private _slices!: Slices;\n private _types!: Types;\n\n repository: string;\n token: string | undefined;\n\n constructor(repository: string, token?: string) {\n this.repository = repository;\n this.token = token;\n }\n\n async login(email: string, password: string, registeredCallback?: Function) {\n\n if (!this.token) {\n this.token = await this.generateToken(email, password)\n\n registeredCallback ? registeredCallback(this.token) : null;\n }\n\n this.init()\n }\n\n public init() {\n let http:HttpFetch = new HttpFetch(this.token as string)\n\n this._slices = new Slices(this.repository, http);\n this._types = new Types(this.repository, http);\n }\n\n private async generateToken(email: string, password: string) {\n\n console?.warn(\"[WARN] using email login method, an API key is safer.\");\n\n try {\n const response = await login({email, password})\n\n const token = await response.text();\n\n return token;\n } catch (error) {\n throw new Error(`[Custom Type API] Unable to log in with using password & email details`)\n }\n }\n\n public slices(): Slices {\n return this._slices\n }\n\n public types(): Types {\n return this._types\n }\n}\n\nexport default Api;\n","import fetch, { Response } from \"node-fetch\";\n\ninterface Credentials {\n email: string,\n password: string\n}\n\nexport default async (credentials: Credentials): Promise<Response> => {\n try {\n return await fetch(\"https://auth.prismic.io/login\", {\n method: 'POST',\n body: JSON.stringify(credentials),\n redirect: 'follow'\n });\n } catch (error) {\n throw new Error(`[Custom Type API] Unable to perform log in action`)\n }\n}\n"],"names":["Slices","repository","http","this","withHeaders","getOne","slice","_this2","get","sliceRoute","response","json","status","message","search","Error","getAll","_this4","error","insert","post","update","Types","customType","type","_this6","insertResponse","HttpFetch","token","headers","Headers","append","request","method","body","redirect","url","fetch","JSON","stringify","newHeaders","header","_this$headers","has","login","email","password","registeredCallback","init","generateToken","_slices","_types","console","warn","credentials","text","slices","types"],"mappings":"0MAMMA,aAIJ,WAAYC,EAAoBC,GAC9BC,KAAKF,WAAaA,EAClBE,KAAKD,KAAOA,EAAKE,YAAY,CAC3BH,WAAcE,KAAKF,wCAIVI,gBAAOC,aAGcH,gEAAAI,EAAKL,KAAKM,ICbtC,SAACF,kDAA2DA,EDalBG,CAAgBH,8DAAtDI,wCAY+BA,EAASC,4BAVtB,MAApBD,EAASE,8BACMF,EAASC,sBAAtBA,GAIJ,YAFcA,SAAAA,EAAME,SAENC,OAAO,4EACjB,IAAIC,MAAM,gCACV,IAAIA,MAAM,oEAOhB,UAAUA,4CAA4CT,4CAI7CU,4BAGcb,gEAAAc,EAAKf,KAAKM,ICrC9B,wDDqCGE,0BAE6BA,EAASC,oBAGrCO,GACP,UAAUH,MAAMG,4CAIPC,gBAAOb,8BAELH,KAAKD,KAAKkB,KC/CjB,+CD+CyCd,0CAGpCe,gBAAOf,8BAELH,KAAKD,KAAKkB,KCnDjB,+CDmDyCd,wIEtD7CgB,aAIJ,WAAYrB,EAAoBC,GAC9BC,KAAKF,WAAaA,EAElBE,KAAKD,KAAOA,EAAKE,YAAY,CAC3BH,WAAcE,KAAKF,wCAIVI,gBAAOkB,aAGOpB,gEAAAI,EAAKL,KAAKM,KDP9BgB,ECOiDD,gDDPcC,mBCO5Dd,0BAEuBA,EAASC,UDTpC,IAACa,YCYIN,GACP,UAAUH,MAAMG,4CAIPF,4BAGcb,gEAAAc,EAAKf,KAAKM,IDrB9B,6DCqBGE,0BAC2BA,EAASC,oBAGnCO,GACP,UAAUH,MAAMG,4CAKPC,gBAAOI,aAQapB,KAN/B,GAA+B,iBAApBoB,EAAWZ,KACpB,UAAUI,MAAM,wIAKaU,EAAKvB,KAAKkB,KDrCnC,oDCqC0DG,kBAAxDG,0BACeA,EAAef,oBAI7BO,GACP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