UNPKG

@wordpress/core-data

Version:
8 lines (7 loc) 4.72 kB
{ "version": 3, "sources": ["../../src/queried-data/get-query-parts.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addQueryArgs } from '@wordpress/url';\n\n/**\n * Internal dependencies\n */\nimport { withWeakMapCache, getNormalizedCommaSeparable } from '../utils';\n\n/**\n * An object of properties describing a specific query.\n *\n * @typedef {Object} WPQueriedDataQueryParts\n *\n * @property {number} page The query page (1-based index, default 1).\n * @property {number} perPage Items per page for query (default 10).\n * @property {string} stableKey An encoded stable string of all non-\n * pagination, non-fields query parameters.\n * @property {?(string[])} fields Target subset of fields to derive from\n * item objects.\n * @property {?(number[])} include Specific item IDs to include.\n * @property {string} context Scope under which the request is made;\n * determines returned fields in response.\n */\n\n/**\n * Given a query object, returns an object of parts, including pagination\n * details (`page` and `perPage`, or default values). All other properties are\n * encoded into a stable (idempotent) `stableKey` value.\n *\n * @param {Object} query Optional query object.\n *\n * @return {WPQueriedDataQueryParts} Query parts.\n */\nexport function getQueryParts( query ) {\n\t/**\n\t * @type {WPQueriedDataQueryParts}\n\t */\n\tconst parts = {\n\t\tstableKey: '',\n\t\tpage: 1,\n\t\tperPage: 10,\n\t\tfields: null,\n\t\tinclude: null,\n\t\tcontext: 'default',\n\t};\n\n\t// Ensure stable key by sorting keys. Also more efficient for iterating.\n\tconst keys = Object.keys( query ).sort();\n\n\tfor ( let i = 0; i < keys.length; i++ ) {\n\t\tconst key = keys[ i ];\n\t\tlet value = query[ key ];\n\n\t\tswitch ( key ) {\n\t\t\tcase 'page':\n\t\t\t\tparts[ key ] = Number( value );\n\t\t\t\tbreak;\n\n\t\t\tcase 'per_page':\n\t\t\t\tparts.perPage = Number( value );\n\t\t\t\tbreak;\n\n\t\t\tcase 'context':\n\t\t\t\tparts.context = value;\n\t\t\t\tbreak;\n\n\t\t\tdefault:\n\t\t\t\t// While in theory, we could exclude \"_fields\" from the stableKey\n\t\t\t\t// because two request with different fields have the same results\n\t\t\t\t// We're not able to ensure that because the server can decide to omit\n\t\t\t\t// fields from the response even if we explicitly asked for it.\n\t\t\t\t// Example: Asking for titles in posts without title support.\n\t\t\t\tif ( key === '_fields' ) {\n\t\t\t\t\tparts.fields = getNormalizedCommaSeparable( value ) ?? [];\n\t\t\t\t\t// Make sure to normalize value for `stableKey`\n\t\t\t\t\tvalue = parts.fields.join();\n\t\t\t\t}\n\n\t\t\t\t// Two requests with different include values cannot have same results.\n\t\t\t\tif ( key === 'include' ) {\n\t\t\t\t\tif ( typeof value === 'number' ) {\n\t\t\t\t\t\tvalue = value.toString();\n\t\t\t\t\t}\n\t\t\t\t\tparts.include = (\n\t\t\t\t\t\tgetNormalizedCommaSeparable( value ) ?? []\n\t\t\t\t\t).map( Number );\n\t\t\t\t\t// Normalize value for `stableKey`.\n\t\t\t\t\tvalue = parts.include.join();\n\t\t\t\t}\n\n\t\t\t\t// While it could be any deterministic string, for simplicity's\n\t\t\t\t// sake mimic querystring encoding for stable key.\n\t\t\t\t//\n\t\t\t\t// TODO: For consistency with PHP implementation, addQueryArgs\n\t\t\t\t// should accept a key value pair, which may optimize its\n\t\t\t\t// implementation for our use here, vs. iterating an object\n\t\t\t\t// with only a single key.\n\t\t\t\tparts.stableKey +=\n\t\t\t\t\t( parts.stableKey ? '&' : '' ) +\n\t\t\t\t\taddQueryArgs( '', { [ key ]: value } ).slice( 1 );\n\t\t}\n\t}\n\n\treturn parts;\n}\n\nexport default withWeakMapCache( getQueryParts );\n"], "mappings": ";AAGA,SAAS,oBAAoB;AAK7B,SAAS,kBAAkB,mCAAmC;AA2BvD,SAAS,cAAe,OAAQ;AAItC,QAAM,QAAQ;AAAA,IACb,WAAW;AAAA,IACX,MAAM;AAAA,IACN,SAAS;AAAA,IACT,QAAQ;AAAA,IACR,SAAS;AAAA,IACT,SAAS;AAAA,EACV;AAGA,QAAM,OAAO,OAAO,KAAM,KAAM,EAAE,KAAK;AAEvC,WAAU,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAM;AACvC,UAAM,MAAM,KAAM,CAAE;AACpB,QAAI,QAAQ,MAAO,GAAI;AAEvB,YAAS,KAAM;AAAA,MACd,KAAK;AACJ,cAAO,GAAI,IAAI,OAAQ,KAAM;AAC7B;AAAA,MAED,KAAK;AACJ,cAAM,UAAU,OAAQ,KAAM;AAC9B;AAAA,MAED,KAAK;AACJ,cAAM,UAAU;AAChB;AAAA,MAED;AAMC,YAAK,QAAQ,WAAY;AACxB,gBAAM,SAAS,4BAA6B,KAAM,KAAK,CAAC;AAExD,kBAAQ,MAAM,OAAO,KAAK;AAAA,QAC3B;AAGA,YAAK,QAAQ,WAAY;AACxB,cAAK,OAAO,UAAU,UAAW;AAChC,oBAAQ,MAAM,SAAS;AAAA,UACxB;AACA,gBAAM,WACL,4BAA6B,KAAM,KAAK,CAAC,GACxC,IAAK,MAAO;AAEd,kBAAQ,MAAM,QAAQ,KAAK;AAAA,QAC5B;AASA,cAAM,cACH,MAAM,YAAY,MAAM,MAC1B,aAAc,IAAI,EAAE,CAAE,GAAI,GAAG,MAAM,CAAE,EAAE,MAAO,CAAE;AAAA,IACnD;AAAA,EACD;AAEA,SAAO;AACR;AAEA,IAAO,0BAAQ,iBAAkB,aAAc;", "names": [] }