@shopify/shopify-api
Version:
Shopify API Library for Node - accelerate development with support for authentication, graphql proxy, webhooks
1 lines • 5.31 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["../../../../../../../lib/auth/scopes/index.ts"],"sourcesContent":["/**\n * A class that represents a set of access token scopes.\n */\nclass AuthScopes {\n public static SCOPE_DELIMITER = ',';\n\n private compressedScopes: Set<string>;\n private expandedScopes: Set<string>;\n private originalScopes: Set<string>;\n\n constructor(scopes: string | string[] | AuthScopes | undefined) {\n let scopesArray: string[] = [];\n if (typeof scopes === 'string') {\n scopesArray = scopes.split(\n new RegExp(`${AuthScopes.SCOPE_DELIMITER}\\\\s*`),\n );\n } else if (Array.isArray(scopes)) {\n scopesArray = scopes;\n } else if (scopes) {\n scopesArray = Array.from(scopes.expandedScopes);\n }\n\n scopesArray = scopesArray\n .map((scope) => scope.trim())\n .filter((scope) => scope.length);\n\n const impliedScopes = this.getImpliedScopes(scopesArray);\n\n const scopeSet = new Set(scopesArray);\n const impliedSet = new Set(impliedScopes);\n\n this.compressedScopes = new Set(\n [...scopeSet].filter((x) => !impliedSet.has(x)),\n );\n this.expandedScopes = new Set([...scopeSet, ...impliedSet]);\n this.originalScopes = scopeSet;\n }\n\n /**\n * Checks whether the current set of scopes includes the given one.\n */\n public has(scope: string | string[] | AuthScopes | undefined) {\n let other: AuthScopes;\n\n if (scope instanceof AuthScopes) {\n other = scope;\n } else {\n other = new AuthScopes(scope);\n }\n\n return (\n other.toArray().filter((x) => !this.expandedScopes.has(x)).length === 0\n );\n }\n\n /**\n * Checks whether the current set of scopes equals the given one.\n */\n public equals(otherScopes: string | string[] | AuthScopes | undefined) {\n let other: AuthScopes;\n\n if (otherScopes instanceof AuthScopes) {\n other = otherScopes;\n } else {\n other = new AuthScopes(otherScopes);\n }\n\n return (\n this.compressedScopes.size === other.compressedScopes.size &&\n this.toArray().filter((x) => !other.has(x)).length === 0\n );\n }\n\n /**\n * Returns a comma-separated string with the current set of scopes.\n */\n public toString() {\n return this.toArray().join(AuthScopes.SCOPE_DELIMITER);\n }\n\n /**\n * Returns an array with the current set of scopes.\n */\n public toArray(returnOriginalScopes = false) {\n return returnOriginalScopes\n ? [...this.originalScopes]\n : [...this.compressedScopes];\n }\n\n private getImpliedScopes(scopesArray: string[]): string[] {\n return scopesArray.reduce((array: string[], current: string) => {\n const matches = current.match(/^(unauthenticated_)?write_(.*)$/);\n if (matches) {\n array.push(`${matches[1] ? matches[1] : ''}read_${matches[2]}`);\n }\n\n return array;\n }, []);\n }\n}\n\nexport {AuthScopes};\n"],"names":[],"mappings":"AAAA;;AAEG;AACH,MAAM,UAAU,CAAA;AACP,IAAA,OAAO,eAAe,GAAG,GAAG;AAE3B,IAAA,gBAAgB;AAChB,IAAA,cAAc;AACd,IAAA,cAAc;AAEtB,IAAA,WAAA,CAAY,MAAkD,EAAA;QAC5D,IAAI,WAAW,GAAa,EAAE;AAC9B,QAAA,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;AAC9B,YAAA,WAAW,GAAG,MAAM,CAAC,KAAK,CACxB,IAAI,MAAM,CAAC,CAAA,EAAG,UAAU,CAAC,eAAe,CAAA,IAAA,CAAM,CAAC,CAChD;QACH;AAAO,aAAA,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;YAChC,WAAW,GAAG,MAAM;QACtB;aAAO,IAAI,MAAM,EAAE;YACjB,WAAW,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC;QACjD;AAEA,QAAA,WAAW,GAAG;aACX,GAAG,CAAC,CAAC,KAAK,KAAK,KAAK,CAAC,IAAI,EAAE;aAC3B,MAAM,CAAC,CAAC,KAAK,KAAK,KAAK,CAAC,MAAM,CAAC;QAElC,MAAM,aAAa,GAAG,IAAI,CAAC,gBAAgB,CAAC,WAAW,CAAC;AAExD,QAAA,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,WAAW,CAAC;AACrC,QAAA,MAAM,UAAU,GAAG,IAAI,GAAG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